Spingboot 使用typeHandler实现自动转换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 启动就可以了哦!

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值