您的位置:首页 > 数据库

hibernate4操作数据库blob案例

2015-09-17 11:45 477 查看
由于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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: