Mybatis 通用JSON Type Handler

20 篇文章 0 订阅

Mybatis通用JSON Type Handler

问题

数据库中的json取出来给前端结果发生了转移
想数据库是什么东西,给前端的就是什么

数据库中===>
question字段类型为json
question字段值为 "https://gitee.com/smallweigit/avue-plugin-ueditor/raw/master/packages/demo/demo.png"

Postman中===>
question的值"question": "\"https://gitee.com/smallweigit/avue-plugin-ueditor/raw/master/packages/demo/demo.png\""



实现

  • 写一个通用JSON Type Handler
  • 在Mybatis中引用这个文件



1、写一个JSON Type Handler

package com.ruoyi.psychological.domain;


import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize;

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

/**
 * mapper里json型字段到类的映射。
 * 用法一:
 * 入库:#{jsonDataField, typeHandler=com.adu.spring_test.mybatis.typehandler.JsonTypeHandler}
 * 出库:
 * <resultMap>
 * <result property="jsonDataField" column="json_data_field" javaType="com.xxx.MyClass" typeHandler="com.adu.spring_test.mybatis.typehandler.JsonTypeHandler"/>
 * </resultMap>
 *
 * 用法二:
 * 1)在mybatis-config.xml中指定handler:
 *      <typeHandlers>
 *              <typeHandler handler="com.adu.spring_test.mybatis.typehandler.JsonTypeHandler" javaType="com.xxx.MyClass"/>
 *      </typeHandlers>
 * 2)在MyClassMapper.xml里直接select/update/insert。
 *
 *
 */
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(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, false);
        mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
    }
}




2、在Mybatis中引用这个文件

  • 映射中 typeHandler="com.ruoyi.psychological.domain.JsonTypeHandler"
    typeHandler=com.ruoyi.psychological.domain.JsonTypeHandler
    <resultMap type="com.ruoyi.course.domain.Exam" id="ExamResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="relationId"    column="relation_id"    />
        <result property="relationType"    column="relation_type"    />
        <result property="cover"    column="cover"    />
        <result property="description"    column="description"    />
        <result property="question"    column="question"   typeHandler="com.ruoyi.psychological.domain.JsonTypeHandler" />
        <result property="startAt"    column="start_at"    />
        <result property="endAt"    column="end_at"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="deleteTime"    column="delete_time"    />
        <result property="radio"    column="radio"    />
        <result property="limitNumber"    column="limit_number"    />
    </resultMap>



   <sql id="selectExamVo">
        select id, name, relation_id, relation_type, cover, description, question, start_at, end_at, create_time, update_time, delete_time, radio ,limit_number from exam
    </sql>

    <select id="selectExamList" parameterType="com.ruoyi.course.domain.Exam" resultMap="ExamResult">
        <include refid="selectExamVo"/>
        <where>  
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="relationId != null "> and relation_id = #{relationId}</if>
            <if test="relationType != null  and relationType != ''"> and relation_type = #{relationType}</if>
            <if test="cover != null  and cover != ''"> and cover = #{cover}</if>
            <if test="description != null  and description != ''"> and description = #{description}</if>
            <if test="question != null  and question != ''"> and question = #{question,typeHandler=com.ruoyi.psychological.domain.JsonTypeHandler}</if>
            <if test="startAt != null "> and start_at = #{startAt}</if>
            <if test="endAt != null "> and end_at = #{endAt}</if>
            <if test="deleteTime != null "> and delete_time = #{deleteTime}</if>
            <if test="id != null "> and id = #{id}</if>
            <if test="limitNumber != null "> and limit_number = #{limitNumber}</if>
        </where>
    </select>

结束词

记录一下这一步的这个脚印

冲冲冲,这个问题耗时5h

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis 字典转换 TypeHandler 是一种用于处理数据库中字段和 Java 实体属性之间的转换的机制。它允许开发人员在数据库中存储枚举值或其他常量,并在 MyBatis 查询时将其转换为相应的 Java 对象。 通常,在数据库设计中,我们会将某些字段的取值限制为预定义的有限选项,通常以整数或字符串形式存储在数据库中。然而,在 Java 代码中,我们更倾向于使用枚举类型或其他自定义对象来表示这些选项。 为了解决数据库字段和 Java 对象之间的转换问题,MyBatis 提供了 TypeHandler 接口。该接口定义了将字段值转换为 Java 对象和将 Java 对象转换为字段值的方法。开发人员可以根据自己的需求实现该接口并注册自己的 TypeHandler。 当 MyBatis 执行查询时,如果遇到了定义了 TypeHandler 的字段,它将使用相应的 TypeHandler 对象来处理字段的转换。类型转换可以是双向的,也就是说可以将 Java 对象转换为数据库字段,以及将数据库字段转换为 Java 对象。 字典转换的一个典型应用场景是将数据库中的整数值转换为对应的枚举类型。通过实现自定义的 TypeHandler,开发人员可以将数据库表中的整数字段映射为相应的枚举对象,从而在程序中更方便地使用枚举值。这种转换可以在查询结果映射时自动进行,也可以在参数设置时手动进行。 总之,MyBatis 字典转换 TypeHandler 是一项非常实用的功能,它允许我们在数据库和 Java 对象之间进行灵活的转换,使程序开发更加方便和高效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值