mybatis查询json格式字段
时间: 2023-10-14 09:19:51 浏览: 43
在 MyBatis 中查询 JSON 格式字段,需要使用 MyBatis 的 TypeHandler 来处理。首先需要在 MyBatis 的配置文件中注册一个 TypeHandler:
```
<typeHandlers>
<typeHandler handler="org.apache.ibatis.type.JsonTypeHandler" javaType="java.lang.Object"/>
</typeHandlers>
```
然后在 Mapper.xml 文件中,使用该 TypeHandler 来处理 JSON 字段:
```
<resultMap id="userMap" type="User">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="info" column="info" typeHandler="org.apache.ibatis.type.JsonTypeHandler"/>
</resultMap>
<select id="getUser" resultMap="userMap">
select id, name, info from user where id = #{id}
</select>
```
在上面的例子中,我们使用了 `org.apache.ibatis.type.JsonTypeHandler` 来处理 `info` 字段,它能够将 JSON 格式的字符串转换为 Java 对象。在 Mapper.xml 文件中,需要将该 TypeHandler 与 `info` 字段进行关联,这样 MyBatis 才能正确地调用该 TypeHandler 进行处理。