代码如下:
public byte[] blob2ByteArr(Blob blob) throws Exception {
byte[] b = null;
try {
if (blob != null) {
long in = 0;
b = blob.getBytes(in, (int) (blob.length()));
}
} catch (Exception e) {
e.printStackTrace();
throw new Exception("fault");
}
return b;
}
Spring的applicationContext.xml文件中:
实体中将blob数据改成byte数组
实体映射文件中:
设置相应的protperty节点的type属性的值为type="org.springframework.orm.hibernate3.support.BlobByteArrayType“
String hql = "from tablename where pkid = ?"; QueryResult qr = super.baseDAO.queryObjects(hql,new Object[]{pkid} ); List<对象名> mylist = qr.getResult(); if (msmylist.size() > 0)
return (对象名)mylist.get(0); return null;
实体类中将blob字段的对象定义为byte[]类型
private byte[] context;
正常取值就可以了
Java代码实现方法
public byte[] blob2ByteArr(Blob blob) throws Exception {
byte[] b = null;
try {
if (blob != null) {
long in = 0;
b = blob.getBytes(in, (int) (blob.length()));
}
} catch (Exception e) {
e.printStackTrace();
throw new Exception("fault");
}
return b;
}
应该可以直接映射到byte[]数组的吧