一.了解clob使用场景和使用方式:
1.为何实用Clob:
Oracle数据库当需要存入大数据量(大于4000)时,varchar2不够用,可以使用clob,本文描述clob怎么和hibernate一起使用。
2.Clob类型的属性的赋值方式String转Clob:
String content = request.getParameter(“content”);//1.从request请求中取值(String类型的)
Clob clob = Hibernate.createClob(content);//2.通过hibernate将string转化为clob
news.setContent(clob);//3.给实体类对应属性赋值
3.Clob类型的属性的取值方式Clob转String:
List<News> list = query.addEntity(News.class).list();//1.从数据库取值
News news = (News)list.get(0);//2.取News对象
String content = ClobUtil.ClobToString(news.getContent());//3.将news对象中的clob类型的content转化为String字符串
二.实例分析:
1.建数据表:
2.创建实体类和映射文件:
public class News {
private Long id;
private String dir;
private String fileName;
private String title;
private String tag;
private String imgUrl;
private Integer type;
private String creator;
private Date createDate;
private String remark;
private Integer ishot;
private Clob content;
//get和set方法省略…
}
hibernate配置文件如下:
<?xml version=”1.0″?>
<!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate/Hibernate Mapping DTD 3.0//EN”
“http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>
<!– Generated 2014-11-28 9:58:10 by Hibernate Tools 3.4.0.CR1 –>
<hibernate-mapping>
<class name=”net.itxm.entity.News” table=”NEWS”>
<id name=”id” column=”ID”>
<generator class=”sequence”>
<param name=”sequence”>SQ_NEWS</param>
</generator>
</id>
<property name=”dir” type=”java.lang.String”>
<column name=”DIR” />
</property>
<property name=”fileName” type=”java.lang.String”>
<column name=”FILE_NAME” />
</property>
<property name=”title” type=”java.lang.String”>
<column name=”TITLE” />
</property>
<property name=”tag” type=”java.lang.String”>
<column name=”TAG” />
</property>
<property name=”imgUrl” type=”java.lang.String”>
<column name=”IMG_URL” />
</property>
<property name=”type” type=”java.lang.Integer”>
<column name=”TYPE” />
</property>
<property name=”creator” type=”java.lang.String”>
<column name=”CREATOR” />
</property>
<property name=”createDate” type=”java.util.Date”>
<column name=”CREATE_DATE” />
</property>
<property name=”remark” type=”java.lang.String”>
<column name=”REMARK” />
</property>
<property name=”ishot” type=”java.lang.Integer”>
<column name=”ISHOT” />
</property>
<property name=”content” type=”java.sql.Clob”>
<column name=”CONTENT” />
</property>
</class>
</hibernate-mapping>
3.增加News的Servlet:需要对String数据转化成Clob数据
String title = request.getParameter(“title”);
String tag = request.getParameter(“tag”);
String content = request.getParameter(“content”);
String typeStr = request.getParameter(“type”);
String ishotStr = request.getParameter(“ishot”);
int type = Integer.valueOf(typeStr);
int ishot = 0;
if(“on”.equals(ishotStr)){
ishot = 1;
}
Clob clob = Hibernate.createClob(content);
INewsDao newsDao = new NewsDaoImpl();
News news = new News();
news.setDir(htmlDir + detailDir);
news.setFileName(fileName);
news.setCreateDate(createDate);
news.setCreator(user.getUsername());
news.setTitle(title);
news.setTag(tag);
news.setType(type);
news.setIshot(ishot);
news.setContent(clob);
newsDao.saveNews(news);
注意:要对request获取的字符串用hibernate转化为clob类型
4.从数据库获取news中的Content:—— 需要对String数据转化成Clob数据
public String getNewsContentById(int id) {
Session s = null;
News news = null;
String content = null;
try {
s = HibernateUtil.getSession();
s.beginTransaction();
StringBuffer sb = new StringBuffer(“select * from news where id = ” + id);
SQLQuery query = s.createSQLQuery(sb.toString());
news = (News) query.addEntity(News.class).uniqueResult();
content = ClobUtil.ClobToString(news.getContent());
s.getTransaction().commit();
} catch (Exception e) {
logger.error(e.toString());
HibernateUtil.endSession(s);
} finally {
HibernateUtil.endSession(s);
}
return content;
}
注意:要对获取到的实体类中的Clob类型数据转化为String类型
5.Clob工具类:
public class ClobUtil {
public static String ClobToString(Clob clob) {
String clobStr = “”;
Reader is = null;
try {
is = clob.getCharacterStream();
// 得到流
BufferedReader br = new BufferedReader(is);
String s = null;
s = br.readLine();
StringBuffer sb = new StringBuffer();
//执行循环将字符串全部取出赋值给StringBuffer,由StringBuffer转成String
while (s != null) {
sb.append(s);
s = br.readLine();
}
clobStr = sb.toString();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return clobStr;
}
}
转载请注明来源网站:blog.ytso.com谢谢!
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/14687.html