mybatis-typeHandler解决表字段中json映射对象问题

1、表实体

package com.***;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import java.util.Date;

@Data
public class Bottom {
    /**
     * id
     */
    private String id;
    private String code;
    /**
     * {
     *     "time_range":[
     *         "06:00:00",
     *         "22:30:00"
     *     ],
     *     "is_open":"1/0",
     *     "support_system":"ios/android/all/other"
     * }
     */
    @JsonProperty(value = "config_json")
    private BottomSubClass configJson;
}

 

2、json映射类

package com.***;
import com.alibaba.fastjson.JSONArray;
import lombok.Data;
@Data
public class BottomSubClass {
    private String is_open;
    private String support_system;
    private JSONArray time_range;
}

 

3、mybatis-config.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="cacheEnabled" value="false"/>
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="multipleResultSetsEnabled" value="true"/>
        <setting name="useColumnLabel" value="true"/>
        <setting name="useGeneratedKeys" value="false"/>
        <setting name="autoMappingBehavior" value="PARTIAL"/>
        <setting name="defaultExecutorType" value="SIMPLE"/>
        <setting name="defaultStatementTimeout" value="25"/>
        <setting name="safeRowBoundsEnabled" value="false"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <setting name="localCacheScope" value="STATEMENT"/>
        <setting name="jdbcTypeForNull" value="OTHER"/>
        <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
    </settings>
    <typeHandlers>
        <typeHandler javaType="string" handler="com.***.StringTypeUtf8mb4Handler"/>
        <typeHandler handler="com.***.typehandler.BottomSubClassTypeHandler"/>
    </typeHandlers>
    <plugins>
        <plugin interceptor="com.***.mybatis.interceptor.MasterSlaveInterceptor"/>
    </plugins>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="org.h2.Driver"/>
                <property name="url" value="jdbc:h2:mem:blog;DB_CLOSE_DELAY=-1"/>
                <property name="username" value=""/>
                <property name="password" value=""/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <package name="com.***.mapper"/>
    </mappers>
</configuration>

 

4、TypeHandler类实现

package com.***.typehandler;
import com.alibaba.fastjson.JSON;
import com.***.BottomSubClass;
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 BottomSubClassTypeHandler extends BaseTypeHandler<BottomSubClass>{

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, BottomSubClass bottom, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, JSON.toJSONString(bottom));
    }

    @Override
    public FlowCoverBottomConfigJson getNullableResult(ResultSet resultSet, String s) throws SQLException {
        return JSON.parseObject(resultSet.getString(s), BottomSubClass.class);
    }

    @Override
    public FlowCoverBottomConfigJson getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return JSON.parseObject(resultSet.getString(i), BottomSubClass.class);
    }

    @Override
    public FlowCoverBottomConfigJson getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        return JSON.parseObject(callableStatement.getString(i), BottomSubClass.class);
    }
}

 

5、Mapper类实现

package com.***.mapper;
import com.***.Bottom;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface BottomMapper {
    @Select("SELECT * FROM bottom")
    List<Bottom> findAllBottom();
@Insert("<script>" + " INSERT INTO bottom ( code, config_json ) " + " VALUES " + " <foreach collection=\"list\" item='item' index=\"list_index\" separator = \",\" > " + " (#{item.code}, #{item.configJson})" + " </foreach>" + "</script>") int insertList(@Param("list") List<FlowCoverBottom> vos); }

 

posted @ 2023-02-20 10:40  GL_BKY  阅读(421)  评论(0编辑  收藏  举报