mybatis-plus自定义字段类型,对象与JSON转换不生效,查询为空,插入异常的问题及解决办法

1.首先,自定义类型转换器(根据你的功能来)继承BaseTypeHandler,下面是对象与json格式转换例子:

@MappedJdbcTypes(JdbcType.VARCHAR)
@Slf4j
@MappedTypes(FrontMetric.class)
public class MetricJsonLocationHandlerType<T> extends BaseTypeHandler<FrontMetric> {

    private static ObjectMapper objectMapper = new ObjectMapper();
    private Class<T> type;

    public MetricJsonLocationHandlerType(Class<T> type) {
        if (type == null) {
            throw new NullPointerException("Type argument cannot be null");
        }
        this.type = type;
        System.out.println("abc");
    }

/*    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, toJsonString(parameter));
    }*/

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, FrontMetric repoAndMetricVo, JdbcType jdbcType) throws SQLException {
        ps.setString(i, toJsonString(repoAndMetricVo));
    }

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


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

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



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

    private FrontMetric parse(String json) {
        try {
            return (FrontMetric) objectMapper.readValue(json, type);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

2.在实体类对应属性上使用@TableField 并指定typeHandler

 

@TableField(value = "metrics",typeHandler = MetricJsonLocationHandlerType.class)
    private FrontMetric metric;

注:需在实体类上指定autoResultMap:

@TableName(value = "tm_front_metric_collect",autoResultMap = true)

3.在对应的xml文件进行相关配置,下面以查询和插入为例:

查询:

<select id="getMetrics" resultMap="repoMetricMap">
        select metrics
        from tm_front_metric_collect
        where json_extract(repo_info, '$.id') = #{repo_id};
</select>

<resultMap id="repoMetricMap" type="repoAndMetricVo">
        <result column="metrics" jdbcType="VARCHAR" property="metric" typeHandler="MetricJsonLocationHandlerType" />
</resultMap>

插入:

<insert id="saveMetrics" >
        insert into tm_front_metric_collect (metrics, repo_info)
         values (#{RepoAndMetric.metric,typeHandler=MetricJsonLocationHandlerType},
                 #{RepoAndMetric.repoInfo,typeHandler=RepoJsonLocationHandlerType});
</insert>

总结:

先自定义字段类型转换器,然后再需要转换的字段上加上TableField注解,并指定typehandler,并在实体类上加上 autoResultMap = true,最后再 xml文件中就行相关配置即可。


版权声明:本文为guokuijj原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。