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>