热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

JAVA操作CLOB类型的数据库字段还是有点麻烦,虽然CLOB不像BLOB那样是用二进制来存值,但不能当成普通的String来操作,当初操作ORACLE这种字段类型时,可把我折腾了半天,现将如何操作的

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. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  3. <hibernate-mapping>
  4.     <class name="kymanage.kybasic.vo.KybasicInfo" table="KYBASIC_INFO" schema="BL">
  5.         <id name="infoId" type="java.lang.Long">
  6.             <column name="INFO_ID" precision="22" scale="0" />
  7.             <generator class="sequence">
  8.             <param name="sequence">INFO_IDparam>
  9.             generator>
  10.         id>
  11.         <property name="infoTitle" type="java.lang.String">
  12.             <column name="INFO_TITLE" length="100" />
  13.         property>
  14.         <property name="infoKeyword" type="java.lang.String">
  15.             <column name="INFO_KEYWORD" length="100" />
  16.         property>
  17.         
  18.         <property name="infoContent" type="java.sql.Clob">
  19.             <column name="INFO_CONTENT" />
  20.         property>
  21.         <property name="infoTop" type="java.lang.Long">
  22.             <column name="INFO_TOP" precision="38" scale="0" />
  23.         property>
  24.         <property name="userSno" type="java.lang.String">
  25.             <column name="USER_SNO" length="100" />
  26.         property>
  27.         <property name="infoTime" type="java.util.Date">
  28.             <column name="INFO_TIME" length="7" />
  29.         property>
  30.         <property name="icId" type="java.lang.Long">
  31.             <column name="IC_ID" precision="38" scale="0" />
  32.         property>
  33.         <property name="infoStandby1" type="java.lang.String">
  34.             <column name="INFO_STANDBY1" length="100" />
  35.         property>
  36.         <property name="infoStandby2" type="java.lang.String">
  37.             <column name="INFO_STANDBY2" length="100" />
  38.         property>
  39.         <property name="infoStandby3" type="java.lang.String">
  40.             <column name="INFO_STANDBY3" length="100" />
  41.         property>
  42.         <property name="infoStandby4" type="java.lang.Long">
  43.             <column name="INFO_STANDBY4" precision="38" scale="0" />
  44.         property>
  45.         <property name="infoStandby5" type="java.lang.Long">
  46.             <column name="INFO_STANDBY5" precision="22" scale="0" />
  47.         property>
  48.         <property name="infoStandby6" type="java.lang.Long">
  49.             <column name="INFO_STANDBY6" precision="22" scale="0" />
  50.         property>
  51.     class>
  52. 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. }

推荐阅读
author-avatar
丢失时
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有