1. 数据库设计 建表,设置字段类型
2.使用myeclipse反向工程 生成hibernate映射文件,需要修改。自动生成的属性名字分别是contentText和contentBinary,我们这里修改在前面加上模块的缩写
3.生成的PO类,也需要修改。修改为与hibernate映射文件中对应的名字,这里还需要加两个string变量来接收页面上的数据,contentText是插入clob字段数据时需要用到的,contentBinary是从数据库中取出值处理之后,再设置到这个变量,方便界面上拿去展现
4. form里面设置如下。下面的红圈圈对应的是jsp页面上控件的name
5. DAO中 主要代码部分,在action中直接调用
public void insertLaws(LawsForm lawsForm) throws Exception {
Laws laws = null ;
try {
laws = new Laws();
BeanUtils.copyProperties(laws, lawsForm);
LawsBclass lawsBclass = new LawsBclass() ;
lawsBclass.setBclassId(lawsForm.getBclassId());
laws.setLawsBclass(lawsBclass);
laws.setLawsContentBinary(Hibernate.createBlob(new byte[1]));
laws.setLawsContentText(Hibernate.createClob(" "));
laws.setIsaudit("0");
this.getHibernateTemplate().save(laws);
this.getHibernateTemplate().flush();
this.getHibernateTemplate().update(laws);
this.getHibernateTemplate().refresh(laws, LockMode.UPGRADE);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
// 向BLOB字段写入内容
SerializableBlob serializableBlob = (SerializableBlob) laws.getLawsContentBinary();
java.sql.Blob javablob = serializableBlob.getWrappedBlob() ;
oracle.sql.BLOB blob = (oracle.sql.BLOB) javablob ;
BufferedInputStream contentBin = new BufferedInputStream(
lawsForm.getContentBinaryFormFile().getInputStream());
BufferedOutputStream contentBout = new BufferedOutputStream(blob
.getBinaryOutputStream());
byte[] buffer = new byte[1024];
int length = 0;
while ((length = contentBin.read(buffer)) > 0) {
contentBout.write(buffer, 0, length);
}
contentBin.close();
contentBout.close();
// 向CLOB字段写入内容
SerializableClob serializableClob = (SerializableClob) laws.getLawsContentText();
java.sql.Clob javaclob = serializableClob.getWrappedClob();
oracle.sql.CLOB clob = (oracle.sql.CLOB)javaclob ;
String contentText = lawsForm.getContentText();
BufferedWriter bw = new BufferedWriter(clob.getCharacterOutputStream());
bw.write(contentText);
bw.close();
}
@SuppressWarnings("deprecation")
public void updateLaws(LawsForm lawsForm) throws Exception {
Laws laws = null ;
try {
laws = new Laws();
BeanUtils.copyProperties(laws, lawsForm);
LawsBclass lawsBclass = new LawsBclass() ;
lawsBclass.setBclassId(lawsForm.getBclassId());
laws.setLawsBclass(lawsBclass);
laws.setLawsContentBinary(Hibernate.createBlob(new byte[1]));
laws.setLawsContentText(Hibernate.createClob(" "));
this.getHibernateTemplate().update(laws);
this.getHibernateTemplate().flush();
this.getHibernateTemplate().refresh(laws, LockMode.UPGRADE);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
// 向BLOB字段写入内容
SerializableBlob serializableBlob = (SerializableBlob) laws.getLawsContentBinary();
java.sql.Blob javablob = serializableBlob.getWrappedBlob() ;
oracle.sql.BLOB blob = (oracle.sql.BLOB) javablob ;
BufferedInputStream contentBin = new BufferedInputStream(
lawsForm.getContentBinaryFormFile().getInputStream());
BufferedOutputStream contentBout = new BufferedOutputStream(blob
.getBinaryOutputStream());
byte[] buffer = new byte[1024];
int length = 0;
while ((length = contentBin.read(buffer)) > 0) {
contentBout.write(buffer, 0, length);
}
contentBin.close();
contentBout.close();
// 向CLOB字段写入内容
SerializableClob serializableClob = (SerializableClob) laws.getLawsContentText();
java.sql.Clob javaclob = serializableClob.getWrappedClob();
oracle.sql.CLOB clob = (oracle.sql.CLOB)javaclob ;
String contentText = lawsForm.getContentText();
BufferedWriter bw = new BufferedWriter(clob.getCharacterOutputStream());
bw.write(contentText);
bw.close();
}
public void updateLaws(Laws laws) throws Exception {
try {
getHibernateTemplate().update(laws);
getHibernateTemplate().flush();
} catch (HibernateOptimisticLockingFailureException ex) {
throw new VersionException(ex);
} catch (DataAccessException ex) {
throw new DAOException(ex);
} catch (Exception ex) {
throw new DAOException(ex);
}
}