最近项目用到了对oracle大字段的读写,小白在这里记录下,方便自己以后用到,也希望对其他朋友有一点帮助。
由于项目的原因,这里的blob只是对
一、读blob
这里对blob的读是直接在数据库建了一个函数Blob_To_Varchar ,这样方便项目里面其它地方用到查询blob:
CREATE OR REPLACE Function Blob_To_Varchar (Blob_In In Blob) Return Varchar2Is V_Varchar Varchar2(4000); V_Start Pls_Integer := 1; V_Buffer Pls_Integer := 4000;Begin If Dbms_Lob.Getlength(Blob_In) Is Null Then Return ''; End If; For I In 1..Ceil(Dbms_Lob.Getlength(Blob_In) / V_Buffer) Loop --当转换出来的字符串乱码时,可尝试用注释掉的函数 --V_Varchar := Utl_Raw.Cast_To_Varchar2(Utl_Raw.Convert(Dbms_Lob.Substr(Blob_In, V_Buffer, V_Start),'SIMPLIFIED CHINESE_CHINA.ZHS16GBK', 'AMERICAN_THE.NETHERLANDS.UTF8')); V_Varchar := Utl_Raw.Cast_To_Varchar2(Dbms_Lob.Substr(Blob_In, V_Buffer, V_Start)); V_Start := V_Start + V_Buffer; End Loop; Return V_Varchar;End Blob_To_Varchar;
直接在sql里面用创建的Blob_To_Varchar函数。
SELECT Blob_To_Varchar(req_tpl) as req_tpl FROM inf_
二、写blob
oracle存大数据的时候,要先插入一个empty_blob()占位符,占到blob字段,其次在查询出来这个大字段用流的方式写入。
首先是插入一个empty_blob()占位符
//插入数据
int insertLogInf = this.logControllerDao.insertLogInfo(params); if(insertLogInf > 0){
//插入大字段数据 for(int i=0;i<2;i++){ if(i == 0){ insertBlob(i,log_id,Const.getStrValue(params, "req_)); }else{ insertBlob(i,log_id,rsp_ }
insertBlob方法(由于要插入2个blob,又不能同时写进去,所以用比较笨的方法循环下)
public void insertBlob(int i , String log_id ,String insertthrows Exception{ BLOB blob= null;
//查询数据 LogInterface= this.logControllerDao.queryBlobLogInfByLogid(log_id); if(i == 0){ blob= (BLOB) retLogInf.getReq_else{ blob= (BLOB) retLogInf.getRsp_= null; try { byte[] data = null; ops = blob); data = insertcatch (Exception e) { e.printStackTrace(); } finally { try { if(ops!=null){ ops.close(); } } catch (IOException e) { e.printStackTrace(); } } }
这里的req_
@Alias("logInterface)public class LogInterfaceprivate String log_id; private String op_code ; private String req_time ; private String rsp_time ; private String ep_address ; private String result_desc ; private Object req_private Object rsp_public String getOp_code() { return op_code; } public void setOp_code(String op_code) { this.op_code = op_code; } public String getReq_time() { return req_time; } public void setReq_time(String req_time) { this.req_time = req_time; } public String getRsp_time() { return rsp_time; } public void setRsp_time(String rsp_time) { this.rsp_time = rsp_time; } public String getEp_address() { return ep_address; } public void setEp_address(String ep_address) { this.ep_address = ep_address; } public String getResult_desc() { return result_desc; } public void setResult_desc(String result_desc) { this.result_desc = result_desc; } public Object getReq_return req_public void setReq_this.req_ req_public Object getRsp_return rsp_public void setRsp_this.rsp_ rsp_public String getLog_id() { return log_id; } public void setLog_id(String log_id) { this.log_id = log_id; } }
对数据库的操作用的是mybatis
<resultMap id="logInterfaceResultMap" type="logInterface> <result property="log_id" column="id"/> <result property="op_code" column="op_code"/> <result property="req_time" column="req_time" /> <result property="rsp_time" column="rsp_time" /> <result property="ep_address" column="ep_address" /> <result property="req_ column="req_ jdbcType="BLOB" /> <result property="rsp_ column="rsp_ jdbcType="BLOB" /> <result property="result_desc" column="result_desc" /> </resultMap> <select id="queryBlobLogInfByLogid" resultType="logInterface parameterType="string" databaseId="oracle"> select * from inf_</select> <insert id="insertLogInfo" parameterType="map" databaseId="oracle"> insert into inf_empty_blob(),empty_blob(),'1',#{result_desc}) </insert>
可能是由于网路问题,这里写入blob的
如果马上查询数据的话,可能不一定有数据,要等会才有数据。
具体是什么原因导致的,还在研究中,有哪位大神要是知道,可以告诉小弟一声,谢谢了。
原标题:oracle blob mybatis xml读写
关键词:xml