前言 在我们系统中编写了SpringBoot+MYSQL 配置支持json数据格式的handler,但是因为配置了多个自定义handler所以在使用中有时正常有时报错接下来将描述下我的做法
原先handler配置
第一个handler —> JsonTypeHandler
@MappedTypes(JSONObject.class)
public class JsonTypeHandler extends BaseTypeHandler<JSONObject> {
代码省略....
}
**第二个handler —> JsonArrayHandler **
@MappedTypes(JSONObject.class)
public class JsonArrayHandler extends BaseTypeHandler<JSONArray>{
代码省略....
}
mybatis 配置
mybatis:
type-handlers-package: com.smart.typeHandlers # 两个handler的共同父包
发现问题
两个handler同时对数据库中的转JSONObject 的数据起作用,导致了有时系统使用第一个handler,有时第二个
解决方案
1.使用resultMap 指定typeHandler
<resultMap id="AA" type="com.smart.pojo.AA">
<id column="id" property="id"></id>
<result column="A" property="a" typeHandler="com.smart.typeHandlers.JsonTypeHandler"></result>
</resultMap>
# 名称瞎编的懂意思就行