hibernate4操作数据库blob案例

来源:互联网 发布:数据库应用程序开发 编辑:程序博客网 时间:2024/05/16 09:51

由于hibernate4废弃hibernate.createClob,只能采用getLobHelper获取,Hibernate4以下的版本均可采用hibernate.createClob

@Transactional
public int uploadCap(CommonsMultipartFile file,CapPackage capPackage){
InputStream fis=null;
   byte[] content=null;
   Transaction transaction=null;
   Session session=null;
session = this.getSessionFactory().openSession();
try {
//将磁盘文件读入到输入流
fis=file.getInputStream();//new FileInputStream("C:\\Users\\wangkk\\Desktop\\1.sql");
//将输入流中的内容存储到字节数组中
content=new byte[fis.available()];
fis.read(content);
Blob blobContent= session.getLobHelper().createBlob(content);
capPackage.setCapContent(blobContent);
this.getHibernateTemplate().save(capPackage);
return 200;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return 0;
}

0 0
原创粉丝点击