字段用 JSONObject类型
private JSONObject parameter;

需要额外添加一个配置类

package com.yh.tg.tg.config;

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

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.exceptions.PersistenceException;
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;

/**
 2. JSON 字段类型处理器
 **/
@Slf4j
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(JSONObject.class)
public class JacksonTypeHandler<T extends Object> extends BaseTypeHandler<T> {
    private static ObjectMapper objectMapper;
    private Class<T> type;

    static {
        objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public JacksonTypeHandler(Class<T> type) {
        if (log.isTraceEnabled()) {
            log.trace("JacksonTypeHandler(" + type + ")");
        }
        if (null == type) {
            throw new PersistenceException("Type argument cannot be null");
        }
        this.type = type;
    }

    private T parse(String json) {
        try {
            if (json == null || json.length() == 0) {
                return null;
            }
            return objectMapper.readValue(json, type);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private String toJsonString(T obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

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

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

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

    @Override
    public void setNonNullParameter(PreparedStatement ps, int columnIndex, T parameter, JdbcType jdbcType)
            throws SQLException {
        ps.setString(columnIndex, toJsonString(parameter));
    }
}

然后mapping里设置resultMap
注意json字段要指定typeHandler=“com.yh.tg.tg.config.JacksonTypeHandler” 处理器

 <resultMap id="BaseResultMap" type="com.yh.tg.tg.model.TgTask">
        <id column="id" property="id" />
        <id column="create_time" property="createTime" />
        <id column="create_user_id" property="createUserId" />
        <id column="update_time" property="updateTime" />
        <id column="update_user_id" property="updateUserId" />
        <id column="is_enable" property="isEnable" />
        <id column="is_delete" property="isDelete" />
        <id column="status" property="status" />
        <id column="type" property="type" />
        <id column="tg_account" property="tgAccount" />
        <id column="execute_count" property="executeCount" />
        <id column="execute_interval" property="executeInterval" />
        <id column="name" property="name" />
        <id column="data_id" property="dataId" />
        <id column="task_id" property="taskId" />
        <id column="parameter" property="parameter" typeHandler="com.yh.tg.tg.config.JacksonTypeHandler"/>
    </resultMap>

查询的时候指定resultMap

  <select id="page" resultMap="BaseResultMap">
        select * from tg_task where bind_userid = #{userId} order by create_time desc
    </select>
上一篇 下一篇