Mybatis对MySQL中BLOB字段的读取

1、在sqlMapConfig中,定义一个typeHandlers

<typeHandlers>
<typeHandler jdbcType="BLOB" javaType="byte[]" handler="org.apache.ibatis.type.BlobTypeHandler"/>
</typeHandlers>

2、在mapper里面定义resultmap的result column

<result column="token" property="token" jdbcType="BLOB"  typeHandler="org.apache.ibatis.type.BlobTypeHandler" />

3、在对应的entity中,将这个字段定义成byte[]类型的就行。

在使用的时候将byte[]转为String就OK了

String correcttoken = new String(userToken.getToken(),"ISO-8859-1");

总结,在Mybatis的官方文档中说明了,框架内置的TypeHandler类型。请参见http://mybatis.github.io/mybatis-3/zh/configuration.html#typeHandlers。同时Mybatis支持自定义typeHandler。

原文地址:https://www.cnblogs.com/pwenlee/p/4762331.html