文章目录
CREATE TABLE `app_order` (
`id` int NOT NULL AUTO_INCREMENT,
`goods` json DEFAULT NULL,
`message` json DEFAULT NULL,
`all_price` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
BaseTypeHandler 自定义基本数据处理
ArrayJsonHandler JSON数组处理
package com.neo.json.handler;
import com.alibaba.fastjson.JSONArray;
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;
/**
* @author neo
* @date 2024/4/26
* 用以mysql中json格式的字段,进行转换的自定义转换器,转换为实体类的JSONArray属性
* <p>
* MappedTypes注解中的类代表此转换器可以自动转换为的java对象
* MappedJdbcTypes注解中设置的是对应的jdbcType
**/
@MappedTypes(JSONArray.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class ArrayJsonHandler extends BaseTypeHandler<JSONArray> {
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, JSONArray objects, JdbcType jdbcType) throws SQLException {
// 设置非空参数
preparedStatement.setString(i, String.valueOf(objects.toJSONString()));
}
@Override
public JSONArray getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
// 根据列名获取可以为空的结果
String sqlJson = resultSet.getString(columnName);
if (null != sqlJson) {
return JSONArray.parseArray(sqlJson);
}
return null;
}
@Override
public JSONArray getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
//根据列索引,获取可以为空的结果
String sqlJson = rs.getString(columnIndex);
if (null != sqlJson) {
return JSONArray.parseArray(sqlJson);
}
return null;
}
@Override
public JSONArray getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
String sqlJson = cs.getString(columnIndex);
if (null != sqlJson) {
return JSONArray.parseArray(sqlJson);
}
return null;
}
}
ObjectJsonHandler JSON对象处理
package com.neo.json.handler;
import com.alibaba.fastjson.JSONArray;
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;
/**
* @author neo
* @date 2024/4/26
* 用以mysql中json格式的字段,进行转换的自定义转换器,转换为实体类的JSONArray属性
* <p>
* MappedTypes注解中的类代表此转换器可以自动转换为的java对象
* MappedJdbcTypes注解中设置的是对应的jdbcType
**/
@MappedTypes(JSONArray.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class ArrayJsonHandler extends BaseTypeHandler<JSONArray> {
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, JSONArray objects, JdbcType jdbcType) throws SQLException {
// 设置非空参数
preparedStatement.setString(i, String.valueOf(objects.toJSONString()));
}
@Override
public JSONArray getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
// 根据列名获取可以为空的结果
String sqlJson = resultSet.getString(columnName);
if (null != sqlJson) {
return JSONArray.parseArray(sqlJson);
}
return null;
}
@Override
public JSONArray getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
//根据列索引,获取可以为空的结果
String sqlJson = rs.getString(columnIndex);
if (null != sqlJson) {
return JSONArray.parseArray(sqlJson);
}
return null;
}
@Override
public JSONArray getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
String sqlJson = cs.getString(columnIndex);
if (null != sqlJson) {
return JSONArray.parseArray(sqlJson);
}
return null;
}
}
MyBatis-Plus 处理
@TableField(**typeHandler** = 自定义基本对象处理.class)
@Data
@TableName("app_order")
public class AppOrder {
@TableField(typeHandler = ObjectJsonHandler.class)
private JSONObject message;
@TableField(typeHandler = ArrayJsonHandler.class)
private JSONArray goods;
@TableId(type = IdType.AUTO)
private Long id;
private BigDecimal allPrice;
}
MyBatis 处理
# 在application.properties中 配置mybaits自定义类型转换类所在的包
mybatis.type-handlers-package=com.neo.json.handler
<resultMap id="BaseResultMap" type="com.neo.json.entity.AppOrder">
<id property="id" column="id"/>
<result column="message" property="message" typeHandler="com.neo.json.handler.ObjectJsonHandler"/>
<result column="goods" property="goods" typeHandler="com.neo.json.handler.ArrayJsonHandler"/>
<result column="allPrice" property="allPrice" jdbcType="DECIMAL"/>
</resultMap>
<insert id="insert" parameterType="com.lxx.campusstore.model.Orders">
insert into app_order (message,goods,allPrice)
values
(
#{message,jdbcType=OTHER,typeHandler="com.neo.json.handler.ObjectJsonHandler"},
#{goods,jdbcType=OTHER,typeHandler="com.neo.json.handler.ArrayJsonHandler"},
#{allPrice,jdbcType=DECIMAL}
)
</insert>
参考:https://blog.csdn.net/LiXiuu/article/details/88970255