SpringBoot集成MyBatis操作MySql8的JSON类型

SpringBoot集成MyBatis操作MySql8的JSON类型

1.定义Json类型转换器:JsonTypeHandler

  • 一个包有一个类型转换器就够了
  • 开箱即用,复制即可
package com.ins.iot.sync.server.handle;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JsonTypeHandler<T extends Object> extends BaseTypeHandler<T> {
    private static final ObjectMapper mapper = new ObjectMapper();
    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 ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, this.toJson(parameter));
    }

    @Override
    public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return this.toObject(rs.getString(columnName), clazz);
    }

    @Override
    public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return this.toObject(rs.getString(columnIndex), clazz);
    }

    @Override
    public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return this.toObject(cs.getString(columnIndex), clazz);
    }

    private String toJson(T object) {
        try {
            return mapper.writeValueAsString(object);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private T toObject(String content, Class<?> clazz) {
        if (content != null && !content.isEmpty()) {
            try {
                return (T) mapper.readValue(content, clazz);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            return null;
        }
    }

    static {
        //mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }
}

2.使用Json类型转换器:JsonTypeHandler

  • entity: 使用JSONObject类型
@Data
@NoArgsConstructor
public class Org {
 
    private Long orgId

    @ApiModelProperty(value = "参数信息")
    private JSONObject paramInfo;
 }
  • mapper
  1. JSONObject 保存到MySql数据库的 json字段
  2. 数据库的 json字段 取出并转化成 JSONObject


   <mapper namespace="com.xxx.xxx.xxx.mapper.OrgMapper">

      <insert id="insertBatch" parameterType="java.util.List">
        insert into org (orgid,,paraminfo)
        values
        <foreach collection="list" item="org" separator=",">
            (
            #{org.orgId},#{org.paramInfo, typeHandler=com.xxx.xx.xxx.handle.JsonTypeHandler}
            )
          </foreach>
      </insert>


       <resultMap id="orgResult" type="com.xxx.xxx.xxx.Org">
             <result property="orgId" column="orgid"/>
            <result property="paramInfo" column="paraminfo" javaType="com.alibaba.fastjson.JSONObject" typeHandler="com.ins.iot.sync.server.handle.JsonTypeHandler"/>
        </resultMap>
       
       <select id="srg" resultMap="orgResult">
        select
              orgid,paraminfo
        from org
    </select>

你可能感兴趣的:(mysql,java,spring,spring,boot,mybatis,json)