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);
}
}