文章目录 1 MySQL中json类型处理1.1 引言1.2 准备建表1.3 Mybatis1.3.1 实体类1.3.2 BaseTypeHandler1.3.3 application.ym
mysql5.7 开始支持json类型字段
点击了解MySQL中JSON类型数据操作
CREATE TABLE `user` ( `id` bigint NOT NULL AUTO_INCREMENT, `name` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL, `content` json DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; 由于 Mybatis 不支持对Mysql的JSON类型处理需要另外处理
package com.test.entity;import com.alibaba.fastjson.JSONObject;@Datapublic class User implements Serializable {private static final long serialVersionUID = 1L;private Long id;private String name;private JSONObject content;} 此处可以选择实现TypeHandler接口或者继承BaseTypeHandler
其中的注解:@MappedTypes和@MappedJdbcTypes是MyBatis框架中的注解,用于指定Java类型和JDBC类型之间的映射关系:
@MappedTypes:用于指定Java类型与数据库中的数据类型之间的映射关系。@MappedJdbcTypes:用于指定JDBC类型与Java类型之间的映射关系package com.test.handler;import com.alibaba.fastjson.JSONObject;import org.apache.ibatis.type.BaseTypeHandler;import org.apache.ibatis.type.JdbcType;import org.apache.ibatis.type.MappedJdbcTypes;import org.apache.ibatis.type.MappedTypes;import java.sql.CallableStatement;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;@MappedTypes(JSONObject.class)@MappedJdbcTypes(JdbcType.VARCHAR)public class MySqlJsonHandler extends BaseTypeHandler<JSONObject>{ @Override public void setNonNullParameter(PreparedStatement ps, int i, JSONObject parameter, JdbcType jdbcType) throws SQLException { ps.setString(i,String.valueOf(parameter.toJSONString())); } @Override public JSONObject getNullableResult(ResultSet rs, String columnName) throws SQLException { String sqlJson = rs.getString(columnName); if (null != sqlJson) { return JSONObject.parseObject(sqlJson); } return null; } @Override public JSONObject getNullableResult(ResultSet rs, int columnIndex) throws SQLException { String sqlJson = rs.getString(columnIndex); if (null != sqlJson) { return JSONObject.parseObject(sqlJson); } return null; } @Override public JSONObject getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { String sqlJson = cs.getNString(columnIndex); if (null != sqlJson) { return JSONObject.parseObject(sqlJson); } return null; }} 在 application.yml 中增加对应配置,把定义的类型转换器注册到mybatis容器中
点击此处了解Mybatis之原理详解
mybatis: type-handlers-package: com.test.handler #增加此项配置 mybatis.type-handlers-package是MyBatis配置文件中的一个属性,用于指定自定义类型处理器的包名。
通过配置mybatis.type-handlers-package属性,可以让MyBatis自动扫描指定包下的所有类型处理器,并将其注册到MyBatis中。这样,在进行数据库操作时,MyBatis就可以自动使用这些类型处理器进行类型转换,从而简化了开发人员的工作
<resultMap id="BaseResultMap" type="com.test.entity.User" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="content" property="content" typeHandler="com.test.handler.MySqlJsonHandler" /> resultMap> 在xml中写sql语句时,需要将使用到 JSON字段的地方配置,以下以插入为例
<insert id="insertTest" parameterType="com.test.entity.User" > insert into user (id, name, content) values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{content,jdbcType=OTHER,typeHandler=com.test.handler.MySqlJsonHandler}) </insert> 在实体类加上@TableName(autoResultMap = true)
在JSON字段映射的属性加上@TableField(typeHandler = JacksonTypeHandler.class);
package com.test.entity;import com.alibaba.fastjson.JSONObject;@Data@TableName(value = "user",autoResultMap = true)public class User implements Serializable {private static final long serialVersionUID = 1L;private Long id;private String name;@TableField(value="content",typeHandler = JacksonTypeHandler.class)private JSONObject content;} 另外,此处的JacksonTypeHandler也可以替换成像上面自定义的MySqlJsonHandler
如果在实体类中 typeHandler 使用的是 Mybatis-Plus 提供的则不需要在注册到 mybatis-plus 容器中,如果像上面使用类似1.3.2自定义的typeHandler ,则要在配置文件中添加属性:mybatis-plus.type-handlers-package来指定自定义的 typeHandler 包的位置
点击此处了解Mybatis之原理详解
在 application.yml 中增加对应配置
mybatis-plus: type-handlers-package: com.test.handler #增加此项配置 如果启动报错:No typehandler found for content…
那么在对应的mapper文件里面对应的JSON字段 添加typeHandler
<resultMap id="BaseResultMap" type="com.test.entity.User"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="content" property="content" typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/> resultMap> 在xml中写sql语句时,需要将使用到 JSON字段的地方配置,也要像Mybatis那样处理
<insert id="insertUser" parameterType="com.test.entity.User"> insert into user values(#{id},#{name},#{content,jdbcType=OTHER,typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler}) </insert> 来源地址:https://blog.csdn.net/u012060033/article/details/130745082
--结束END--
本文标题: Mybatis和Mybatis-Plus对MySQL中json类型处理
本文链接: https://www.lsjlt.com/news/427331.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0