Mybatis自定义类型转换解决mysql blob类型乱码
参考: http://blog.csdn.net/wanyanxgf/article/details/8294485
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
package com.pandy.framework.core.typeHandler;/** * Created by pandy on 16-8-17. */ import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; import java.sql.*; /** * 项目名称: workspace * 功能说明: * 创建者: Pandy, * 邮箱: panyongzheng@163.com, 1453261799@qq.com * 版权: * 官网: * 创建日期: 16-8-17. * 创建时间: 下午5:11. * 修改历史: * ----------------------------------------------- */ public class BlobTypeHandler extends BaseTypeHandler<String> { private static final String DEFAULT_CHARSET = "utf-8"; @Override public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { ByteArrayInputStream bis; try { bis = new ByteArrayInputStream(parameter.getBytes(DEFAULT_CHARSET)); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Blob Encoding Error!"); } ps.setBinaryStream(i, bis, parameter.length()); } @Override public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException { Blob blob = rs.getBlob(columnIndex); 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 String getNullableResult(ResultSet rs, String columnName) throws SQLException { Blob blob = rs.getBlob(columnName); 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 String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { Blob blob = cs.getBlob(columnIndex); 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!"); } } } |
方式一: 直接使用
Mapper文件
1 2 |
<result property="content" column="CONTENT_" typeHandler="com.pandy.framework.core.typeHandler.BlobTypeHandler"/> <result property="template" column="TEMPLATE_" typeHandler="com.pandy.framework.core.typeHandler.BlobTypeHandler"/> |
方式二:使用别名
配置sqlSessionFactory
1 |
<property name="typeAliasesPackage" value="com.pandy.framework.core.typeHandler"></property> |
Mapper文件
1 2 |
<result property="content" column="CONTENT_" typeHandler="blobTypeHandler"/> <result property="template" column="TEMPLATE_" typeHandler="blobTypeHandler"/> |
方式三: typeHandlersPackage或者typeHandlers属性方式
没有配置成功,使用上面第二种方式