Spingboot 使用typeHandler做java对象转换json
背景:使用Springboot+Mybatis+Mysql存放一个VARCHAR类型的java对象传json。但是mybatis不支持json类型,所以通过实现typeHandler,或者继承BaseTypeHandler 实现自动转换java类型。之前有使用springmvc实现过但是springboot没有xml就会有一点区别,不是很熟悉的同学就问springboot怎么使用呢?
首先我们自定义一个typeHandler类
package com.vdadmin.config.mybatis;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vdadmin.model.seoconfig.vo.SeoConfigWindowsVo;
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 org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Component;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @Author: Clint
* @Date: 2020/11/11 15:15
* @Version: 1.0
* @Description:
*/
@MappedTypes(value = {JSONObject.class, SeoConfigWindowsVo.class})//你要转换的类
@MappedJdbcTypes(value = {JdbcType.VARCHAR},includeNullJdbcType = true)
public class JsonTypeHandler<T extends Object> extends BaseTypeHandler<T> {
private Class<T> clazz;
public JsonTypeHandler(Class<T> clazz) {
if (clazz == null){
throw new IllegalArgumentException("Type argument cannot be null");
}
this.clazz = clazz;
}
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, T t, JdbcType jdbcType) throws SQLException {
preparedStatement.setString(i, JSON.toJSONString(t));
}
@Override
public T getNullableResult(ResultSet resultSet, String s) throws SQLException {
String sqlJson = resultSet.getString(s);
if (null != sqlJson) {
return JSONObject.parseObject(sqlJson, clazz);
}
return null;
}
@Override
public T getNullableResult(ResultSet resultSet, int columnIndex) throws SQLException {
String sqlJson = resultSet.getString(columnIndex);
if (null != sqlJson) {
return JSONObject.parseObject(sqlJson, clazz);
}
return null;
}
@Override
public T getNullableResult(CallableStatement callableStatement, int columnIndex) throws SQLException {
String sqlJson = callableStatement.getString(columnIndex);
if (null != sqlJson) {
return JSONObject.parseObject(sqlJson, clazz);
}
return null;
}
}
一个实体类
/**
* @Author: Clint
* @Date: 2020/11/11 11:54
* @Version: 1.0
* @Description: 配置中心
*/
@Data
@Entity
@Table(name = "seo_config")
public class SeoConfig extends BaseEntity {
private Integer id;
private Integer configType;
private Integer clientType;
private SeoConfigWindowsVo content;
}
实体需要转换的类
/**
* @Author: Clint
* @Date: 2020/11/11 11:54
* @Version: 1.0
* @Description: 配置中心
*/
@Data
public class SeoConfigWindowsVo implements Serializable {
private Integer id;
private Integer configType;
private Integer clientType;
private String content;
}
以及一个xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.vdadmin.system.host.seoconfig.dao.SeoConfigDao">
<resultMap id="BaseResultMap" type="com.vdadmin.model.seoconfig.entity.SeoConfig">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="config_type" jdbcType="INTEGER" property="configType" />
<result column="client_type" jdbcType="INTEGER" property="clientType" />
<result column="content" jdbcType="VARCHAR" property="content" typeHandler="com.vdadmin.config.mybatis.JsonTypeHandler"/>
<result column="create_by" jdbcType="VARCHAR" property="createBy" />
<result column="create_date" jdbcType="TIMESTAMP" property="createDate" />
<result column="update_by" jdbcType="VARCHAR" property="updateBy" />
<result column="update_date" jdbcType="TIMESTAMP" property="updateDate" />
</resultMap>
<sql id="Base_Column_List">
id, config_type, client_type, content, create_by, create_date, update_by, update_date
</sql>
<insert id="insertConfig">
insert into seo_config (id, config_type, client_type,
content, create_by, create_date,
update_by, update_date)
values (#{id,jdbcType=INTEGER}, #{configType,jdbcType=INTEGER}, #{clientType,jdbcType=INTEGER},
#{content,typeHandler=com.vdadmin.config.mybatis.JsonTypeHandler}, #{createBy,jdbcType=VARCHAR}, #{createDate,jdbcType=TIMESTAMP},
#{updateBy,jdbcType=VARCHAR}, #{updateDate,jdbcType=TIMESTAMP})
</insert>
</mapper>
装换的字段声明typeHandler="com.vdadmin.config.mybatis.JsonTypeHandler"添加以后才会被spring装配。最后在yml配置一下type-handlers-packagep: com.vdadmin.config.mybatis.JsonTypeHandler 启动就可以了哦!