hibernate在SSH框架操作ORACLE Clob字段类型的读写改

JAVA操作CLOB类型的数据库字段还是有点麻烦,虽然CLOB不像BLOB那样是用二进制来存值,但不能当成普通的String来操作,当初操作ORACLE这种字段类型时,可把我折腾了半天,现将如何操作的步骤写了个比较详细的例子和说明,以供大家参考.

这里只写最关键的几个地方,其它的地方平时该怎么写就怎么写

首先是POJO类:

  1. import java.sql.Clob;
  2. import java.util.Date;
  3. /**
  4. * @author coffee
  5. *
  6. */
  7. public class KybasicInfo implements java.io.Serializable {
  8. private Long infoId;
  9. private String infoTitle;
  10. private String infoKeyword;
  11. private Clob infoContent; //注意这个地方是java.sql.Clob类型的,生成默认是String类型的,需要手工改一下
  12. private String infoContentToString; //这个是和infoContent做转换时,手工添加的一个,不和数据库里的字段进行对应的
  13. private Long infoTop;
  14. private String userSno;
  15. private Date infoTime;
  16. private Long icId;
  17. private String infoStandby1;
  18. private String infoStandby2;
  19. private String infoStandby3;
  20. private Long infoStandby4;
  21. private Long infoStandby5;
  22. private Long infoStandby6;
  23. public KybasicInfo() {
  24. }
  25. public KybasicInfo(String infoTitle, String infoKeyword,
  26.     Clob infoContent, Long infoTop, String userSno,
  27.     Date infoTime, Long icId, String infoStandby1, String infoStandby2,
  28.     String infoStandby3, Long infoStandby4, Long infoStandby5,
  29.     Long infoStandby6) {
  30.    this.infoTitle = infoTitle;
  31.    this.infoKeyword = infoKeyword;
  32.    this.infoContent = infoContent;
  33.    this.infoTop = infoTop;
  34.    this.userSno = userSno;
  35.    this.infoTime = infoTime;
  36.    this.icId=icId;
  37.    this.infoStandby1 = infoStandby1;
  38.    this.infoStandby2 = infoStandby2;
  39.    this.infoStandby3 = infoStandby3;
  40.    this.infoStandby4 = infoStandby4;
  41.    this.infoStandby5 = infoStandby5;
  42.    this.infoStandby6 = infoStandby6;
  43. }
  44. public Long getInfoId() {
  45.    return this.infoId;
  46. }
  47. public void setInfoId(Long infoId) {
  48.    this.infoId = infoId;
  49. }
  50. public String getInfoTitle() {
  51.    return this.infoTitle;
  52. }
  53. public void setInfoTitle(String infoTitle) {
  54.    this.infoTitle = infoTitle;
  55. }
  56. public String getInfoKeyword() {
  57.    return this.infoKeyword;
  58. }
  59. public void setInfoKeyword(String infoKeyword) {
  60.    this.infoKeyword = infoKeyword;
  61. }
  62. public Clob getInfoContent() {
  63.    return this.infoContent;
  64. }
  65. public void setInfoContent(Clob infoContent) {
  66.    this.infoContent = infoContent;
  67. }
  68. public Long getInfoTop() {
  69.    return this.infoTop;
  70. }
  71. public void setInfoTop(Long infoTop) {
  72.    this.infoTop = infoTop;
  73. }
  74. public Date getInfoTime() {
  75.    return this.infoTime;
  76. }
  77. public void setInfoTime(Date infoTime) {
  78.    this.infoTime = infoTime;
  79. }
  80. public String getInfoStandby1() {
  81.    return this.infoStandby1;
  82. }
  83. public void setInfoStandby1(String infoStandby1) {
  84.    this.infoStandby1 = infoStandby1;
  85. }
  86. public String getInfoStandby2() {
  87.    return this.infoStandby2;
  88. }
  89. public void setInfoStandby2(String infoStandby2) {
  90.    this.infoStandby2 = infoStandby2;
  91. }
  92. public String getInfoStandby3() {
  93.    return this.infoStandby3;
  94. }
  95. public void setInfoStandby3(String infoStandby3) {
  96.    this.infoStandby3 = infoStandby3;
  97. }
  98. public Long getInfoStandby4() {
  99.    return this.infoStandby4;
  100. }
  101. public void setInfoStandby4(Long infoStandby4) {
  102.    this.infoStandby4 = infoStandby4;
  103. }
  104. public Long getInfoStandby5() {
  105.    return this.infoStandby5;
  106. }
  107. public void setInfoStandby5(Long infoStandby5) {
  108.    this.infoStandby5 = infoStandby5;
  109. }
  110. public Long getInfoStandby6() {
  111.    return this.infoStandby6;
  112. }
  113. public void setInfoStandby6(Long infoStandby6) {
  114.    this.infoStandby6 = infoStandby6;
  115. }
  116. public String getUserSno() {
  117.    return userSno;
  118. }
  119. public void setUserSno(String userSno) {
  120.    this.userSno = userSno;
  121. }
  122. public Long getIcId() {
  123.    return icId;
  124. }
  125. public void setIcId(Long icId) {
  126.    this.icId = icId;
  127. }
  128. public String getInfoContentToString() {
  129.    return infoContentToString;
  130. }
  131. public void setInfoContentToString(String infoContentToString) {
  132.    this.infoContentToString = infoContentToString;
  133. }
  134. }

hbm.xml配置文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4. <hibernate-mapping>
  5.     <class name="kymanage.kybasic.vo.KybasicInfo" table="KYBASIC_INFO" schema="BL">
  6.         <id name="infoId" type="java.lang.Long">
  7.             <column name="INFO_ID" precision="22" scale="0" />
  8.             <generator class="sequence">
  9.             <param name="sequence">INFO_ID</param>
  10.             </generator>
  11.         </id>
  12.         <property name="infoTitle" type="java.lang.String">
  13.             <column name="INFO_TITLE" length="100" />
  14.         </property>
  15.         <property name="infoKeyword" type="java.lang.String">
  16.             <column name="INFO_KEYWORD" length="100" />
  17.         </property>
  18.         <!--注意:这里要将默认生成的String类型改成java.sql.Clob-->
  19.         <property name="infoContent" type="java.sql.Clob">
  20.             <column name="INFO_CONTENT" />
  21.         </property>
  22.         <property name="infoTop" type="java.lang.Long">
  23.             <column name="INFO_TOP" precision="38" scale="0" />
  24.         </property>
  25.         <property name="userSno" type="java.lang.String">
  26.             <column name="USER_SNO" length="100" />
  27.         </property>
  28.         <property name="infoTime" type="java.util.Date">
  29.             <column name="INFO_TIME" length="7" />
  30.         </property>
  31.         <property name="icId" type="java.lang.Long">
  32.             <column name="IC_ID" precision="38" scale="0" />
  33.         </property>
  34.         <property name="infoStandby1" type="java.lang.String">
  35.             <column name="INFO_STANDBY1" length="100" />
  36.         </property>
  37.         <property name="infoStandby2" type="java.lang.String">
  38.             <column name="INFO_STANDBY2" length="100" />
  39.         </property>
  40.         <property name="infoStandby3" type="java.lang.String">
  41.             <column name="INFO_STANDBY3" length="100" />
  42.         </property>
  43.         <property name="infoStandby4" type="java.lang.Long">
  44.             <column name="INFO_STANDBY4" precision="38" scale="0" />
  45.         </property>
  46.         <property name="infoStandby5" type="java.lang.Long">
  47.             <column name="INFO_STANDBY5" precision="22" scale="0" />
  48.         </property>
  49.         <property name="infoStandby6" type="java.lang.Long">
  50.             <column name="INFO_STANDBY6" precision="22" scale="0" />
  51.         </property>
  52.     </class>
  53. </hibernate-mapping>

 

hibernate DAO类:

 

  1. import oracle.sql.CLOB;
  2. import java.io.IOException;
  3. import java.io.Writer;
  4. import java.sql.Clob;
  5. import java.sql.SQLException;
  6. import java.util.Date;
  7. import java.util.Iterator;
  8. import java.util.List;
  9. import org.apache.commons.logging.Log;
  10. import org.apache.commons.logging.LogFactory;
  11. import org.hibernate.Hibernate;
  12. import org.hibernate.LockMode;
  13. import org.hibernate.Query;
  14. import org.hibernate.Session;
  15. import org.hibernate.Transaction;
  16. import org.hibernate.lob.SerializableClob;
  17. import org.springframework.context.ApplicationContext;
  18. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
  19. /**
  20. * kybasicInfo
  21. * @author coffee
  22. */
  23. public class KybasicInfoDAOImpl extends HibernateDaoSupport {
  24. protected void initDao() {
  25. }
  26. //新增,在action里将表单中的这个大文本按正常情况传过来,也就是传String类型过来,然后,再在DAO里做Clob类型转换,
  27. //这里面可能会有其它层来传参,但最终还是将传过来的Stirng转换成Clob型的,然后通过文件流形式写入到CLOB字段中去
  28. public void save(KybasicInfo kybasicInfo) {
  29.    try {
  30.     Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
  31.     Transaction tran=session.beginTransaction();
  32.     kybasicInfo.setInfoContent(Hibernate.createClob(" "));//注意,这里的参数是个空格,先新增一个空的Clob进去
  33.     session.save(kybasicInfo);
  34.     session.flush();//强制执行
  35.     session.refresh(kybasicInfo,LockMode.UPGRADE);
  36.     SerializableClob sc=(SerializableClob)kybasicInfo.getInfoContent();//kybasicInfo.getInfoContent()是Clob类型的
  37.     Clob wrapclob=sc.getWrappedClob();//这里的Clob是java.sql.Clob
  38.     CLOB clob=(CLOB)wrapclob;//这里的CLOB是oracle.sql.CLOB
  39.     Writer writer=clob.getCharacterOutputStream();
  40.     writer.write(kybasicInfo.getInfoContentToString());//kybasicInfo.getInfoContentToString()是String类型的,在action里就是传这个进来,然后再通过文件流形式写成CLOB字段中
  41.     writer.close();
  42.     session.save(kybasicInfo);
  43.     tran.commit();
  44.    
  45.    } catch (RuntimeException re) {
  46.     throw re;
  47.    } catch (SQLException e) {
  48.     e.printStackTrace();
  49.    } catch (IOException e) {
  50.     e.printStackTrace();
  51.    }
  52. }
  53. //更新,操作的新增是一样的,都是先接收String类型的参数过来,然后再将String的转成CLOB类型的
  54. public void update(KybasicInfo persistentInstance) {
  55.    try {
  56.     Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
  57.     Transaction tran=session.beginTransaction();
  58.     persistentInstance.setInfoContent(Hibernate.createClob(" "));
  59.     session.update(persistentInstance);
  60.     session.flush();
  61.     session.refresh(persistentInstance,LockMode.UPGRADE);
  62.     SerializableClob sc=(SerializableClob)persistentInstance.getInfoContent();
  63.     Clob wrapclob=sc.getWrappedClob();
  64.     CLOB clob=(CLOB)wrapclob;
  65.     Writer writer=clob.getCharacterOutputStream();
  66.     writer.write(persistentInstance.getInfoContentToString());
  67.     writer.close();
  68.     session.update(persistentInstance);
  69.     tran.commit();
  70.    } catch (RuntimeException re) {
  71.     throw re;
  72.    } catch (SQLException e) {
  73.     e.printStackTrace();
  74.    } catch (IOException e) {
  75.     e.printStackTrace();
  76.    }
  77. }
  78. //读取Clob字段值
  79. public KybasicInfo findById(java.lang.Long id) {
  80.    try {
  81.     KybasicInfo instance = (KybasicInfo) getHibernateTemplate().get(
  82.       "kymanage.kybasic.vo.KybasicInfo", id);
  83.     Clob clob=instance.getInfoContent();//取得Clob的值
  84.     if(clob!=null){
  85.      String clobString="";
  86.      try {
  87.       clobString = clob.getSubString(1, (int)clob.length());//将Clob类型的值转换成String类型的值
  88.       instance.setInfoContentToString(clobString);//通过setter方法,设置String值,然后就可以通过instance.getInfoContentToString()来取值了
  89.      } catch (SQLException e) {
  90.       e.printStackTrace();
  91.      }
  92.     }
  93.     return instance;
  94.    } catch (RuntimeException re) {
  95.     throw re;
  96.    }
  97. }
  98. public static KybasicInfoDAOImpl getFromApplicationContext(
  99.     ApplicationContext ctx) {
  100.    return (KybasicInfoDAOImpl) ctx.getBean("KybasicInfoDAO");
  101. }
  102. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值