Oracle的Blob对应String类型

package com.sinosoft.business.base.service;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

/**
 * 解决mybaits 转换ORACLE blob的时候乱码问题
 * 并将此类配置在mybaits的xml文件中
 *
 */
public class MyBlobTypeHandlerOracle extends BaseTypeHandler<Object> {
	
	private static final String DEFAULT_CHARSET = "GBK";
	
	@Override
	public Object getNullableResult(ResultSet arg0, String arg1)
			throws SQLException {
		Blob blob = (Blob) arg0.getBlob(arg1);
        byte[] returnValue = null;
        if (null != blob&&blob.length()>1) {
            returnValue = blob.getBytes(1, (int) blob.length());
        }else{
        	return "";
        }
        try {
           //把byte转化成string
        	System.out.println(new String(returnValue, DEFAULT_CHARSET));
        	System.out.println(new String(returnValue, "GBK"));
            return new String(returnValue, DEFAULT_CHARSET);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Blob Encoding Error!");
        }
	}


	@Override
	public Object getNullableResult(CallableStatement arg0, int arg1)
			throws SQLException {
		    Blob blob = (Blob) arg0.getBlob(arg1);
	        byte[] returnValue = null;
	        if (null != blob) {
	            returnValue = blob.getBytes(1, (int) blob.length());
	        }
	        try {
	            return new String(returnValue, DEFAULT_CHARSET);
	        } catch (UnsupportedEncodingException e) {
	            throw new RuntimeException("Blob Encoding Error!");
	        }
	}

	@Override
	public void setNonNullParameter(PreparedStatement arg0, int arg1,
			Object arg2, JdbcType arg3) throws SQLException {
			ByteArrayInputStream bis;
	        try {
	            //把String转化成byte流
	            bis = new ByteArrayInputStream(((String)arg2).getBytes(DEFAULT_CHARSET));
	        } catch (UnsupportedEncodingException e) {
	            throw new RuntimeException("Blob Encoding Error!");
	        }   
	        arg0.setBinaryStream(arg1, bis, ((String)arg2).length());
	}


	@Override
	public Object getNullableResult(ResultSet arg0, int arg1)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

}
<resultMap type="com.sinosoft.mobile.po.SysMailInBox" id="mobileMailDetailMap">
	<result column="id" property="id"/>
	<result column="sendmailaddress" property="sendmailaddress"/>
	<result column="receivetime" property="receivetime"/>
	<result column="title" property="title"/>
	<result column="thetype" property="thetype"/>
	<result column="isread" property="isread"/>
	<result column="content" property="content" typeHandler="com.sinosoft.business.base.service.MyBlobTypeHandlerOracle"/>
    <collection property="attacmentList" ofType="com.sinosoft.mobile.po.SysMailAttacment">  
	    <result column="fileName" property="fileName"/>  
	    <result column="filePath" property="filePath"/>  
    </collection>  
</resultMap>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值