使用hibernate保存blob大数据,ibatis查询blob大数据
2013-01-29 14:33
525 查看
需求:将对象序列化,保存到数据库中,数据库设计用blob数据类型,使用oracle数据库,使用hibernate保存blob大数据,ibatis查询blob大数据.
部分代码献上:
一、存:
1、要保存的类:classSaveQueryBOimplementsSerializable
2、将对象转换成blob格式:BlobsaveQueryBo=Hibernate.createBlob(SearchUtil.objectToByte(saveQueryBO));
3、hibernate配置:
<propertyname="saveQueryBo"column="S***E_QUERY_BO"type="java.sql.Blob"
not-null="false"
/>
4、save这个就不贴了;完事儿
二、取
1、ibatis配置
<resultproperty="saveQueryBo"column="save_Query_Bo"jdbcType="BLOB"javaType="java.lang.Object"/>
说明下:
javaType="java.lang.Object"必须是这个,不然报你写个handler吧~
2、相应类:
privateObjectsaveQueryBo;
saveQueryBo虽然是blob类型的,但是必须写成object的,同上1点;
3、查询,转换
if(saveQueryBo!=null){
BlobsaveQueryBoBlob=(Blob)saveQueryBo;
return(SaveQueryBO)SearchUtil.byteToObject(SearchUtil.blobToByte(saveQueryBoBlob));
}
完整代码:
importjava.sql.Blob;
publicclassSaveQueryVO{
privateStringtableKey;//主键
privateStringqueryName;//查询名称
privateStringuserCode;//用户编号
privateStringcustType;//客户类型
privateObjectsaveQueryBo;//搜索相关类
privateStringsql;//sql
publicStringgetSql(){
returnsql;
}
publicvoidsetSql(Stringsql){
this.sql=sql;
}
/**
*返回的是BLOB对象,需要强转
*/
publicObjectgetSaveQueryBo(){
returnsaveQueryBo;
}
/**
*返回的是SaveQueryBO对象
*@throwsCrmBaseException
*/
publicSaveQueryBOgetSaveQueryBO()throwsCrmBaseException{
try{
if(saveQueryBo!=null){
BlobsaveQueryBoBlob=(Blob)saveQueryBo;
return (SaveQueryBO)SearchUtil.byteToObject(SearchUtil.blobToByte(saveQueryBoBlob));
}
}catch(Exceptione){
e.printStackTrace();
//TODO
thrownewCrmBaseException("系统错误");
}
returnnull;
}
publicvoidsetSaveQueryBo(ObjectsaveQueryBo){
this.saveQueryBo=saveQueryBo;
}
publicStringgetCustType(){
returncustType;
}
publicvoidsetCustType(StringcustType){
this.custType=custType;
}
publicStringgetTableKey(){
returntableKey;
}
publicvoidsetTableKey(StringtableKey){
this.tableKey=tableKey;
}
publicStringgetQueryName(){
returnqueryName;
}
publicvoidsetQueryName(StringqueryName){
this.queryName=queryName;
}
publicStringgetUserCode(){
returnuserCode;
}
publicvoidsetUserCode(StringuserCode){
this.userCode=userCode;
}
}
importjava.io.BufferedInputStream;
importjava.io.ByteArrayInputStream;
importjava.io.ByteArrayOutputStream;
importjava.io.ObjectInputStream;
importjava.io.ObjectOutputStream;
importjava.sql.Blob;
publicclassSearchUtil{
/**'
*获得搜索主表code
*@return
*/
publicstaticStringgetMainTableCode(){
returnSearchConstant.MAIN_TABLE_CODE_PE_VALUE;
}
/**
*
*@paramcolumnType
*@paramcolumnValue
*@return
*@throwsCrmBaseException
*/
publicstaticStringgetColumValue4Sql(StringcolumnType,StringcolumnValue)throwsCrmBaseException{
//当类型为数字
if(SearchConstant.COLUMNTYPE_NUMBER.equals(columnType)||SearchConstant.COLUMNTYPE_DOUBLE.equals(columnType)){
if(StringUtil.isEmpty(columnValue)){
return"0";
}else{
returncolumnValue;
}
}elseif(SearchConstant.COLUMNTYPE_DATE.equals(columnType)){//当类型为日期
if(StringUtil.isEmpty(columnValue)){
return"''";
}else{
return"to_date('"+columnValue+"','yyyy-MM-dd')";
}
}else{//默认
if(StringUtil.isEmpty(columnValue)){
return"''";
}else{
return"'"+columnValue+"'";
}
}
}
/**
*将对象转化为字节流
*@paramobj
*@return
*@throwsException
*/
publicstaticbyte[]objectToByte(Objectobject)throwsCrmBaseException{
try{
if(object==null)returnnull;
ByteArrayOutputStreambos=newByteArrayOutputStream();
ObjectOutputStreamoos=newObjectOutputStream(bos);
oos.writeObject(object);
byte[]bytes=bos.toByteArray();
bos.close();
oos.close();
returnbytes;
}catch(Exceptione){
e.printStackTrace();
thrownewCrmBaseException(e.getMessage());
}
}
/**
*将字节流转化为对象
*@parambytes
*@return
*@throwsException
*/
publicstaticObjectbyteToObject(byte[]bytes)throwsException{
if(bytes==null)returnnull;
ByteArrayInputStreambis=newByteArrayInputStream(bytes);
ObjectInputStreamois=newObjectInputStream(bis);
Objectobject=ois.readObject();
bis.close();
ois.close();
returnobject;
}
publicstaticbyte[]blobToByte(Blobblob){
if(blob==null)returnnull;
byte[]listByte=null;
try{
BufferedInputStreamis=newBufferedInputStream(blob.getBinaryStream());
intlen=(int)blob.length();
listByte=newbyte[len];
intoffset=0;
intread=0;
while(offset<len&&(read=is.read(listByte,offset,len-offset))>=0){
offset+=read;
}
}catch(Exceptione){
e.printStackTrace();
}
returnlistByte;
}
}
<resultMapid="saveQueryVO"class="SaveQueryVO">
<resultproperty="tableKey"column="table_Key"/>
<resultproperty="queryName"column="query_Name"/>
<resultproperty="userCode"column="user_Code"/>
<resultproperty="custType"column="cust_Type"/>
<resultproperty="saveQueryBo"column="save_Query_Bo"jdbcType="BLOB"javaType="java.lang.Object"/>
<resultproperty="sql"column="sql"/>
</resultMap>
相关文章推荐
- 使用Struts2+Hibernate(HQL)添加数据、查询数据
- C++文本查询程序 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- Hibernate使用原生SQL适应复杂数据查询
- C++文本查询程序 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- 使用hibernate查询时,查询的数据和数据库的不一样问题
- 使用 hibernate 存取大对象数据类型(clob和blob)
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- 使用 hibernate 存取大对象数据类型(clob和blob)
- 使用SSH框架,只能查询数据,不能修改和保存数据的问题
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- Hibernate使用原生SQL适应复杂数据查询
- Hibernate查询数据的几种方式以及HQL用法及参数的几种使用方法
- 如何使用jdbc template查询数据,而用ibatis组装结果?
- C++文本查询程序 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- Hibernate用Criteria查询时使用Distinct方法去除重复数据
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- 使用Spring+Hibernate实现Oracle数据库BlOB数据类型的读写
- 使用Spring+Hibernate实现Oracle数据库BlOB数据类型的读写
- 使用hibernate的this.getSession().createSQLQuery(sql).list();方法查询数据时出现查到的数据和想象的不一致,很是郁闷,诡异