Mybatis 通用JSON Type Handler

Mybatis 通用JSON Type Handler,第1张

Mybatis 通用JSON Type Handler 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;


public class JsonTypeHandler extends baseTypeHandler {
    private static final ObjectMapper mapper = new ObjectMapper();
    private Class clazz;

    public JsonTypeHandler(Class 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
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    



   
        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
    

    
结束词

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5563843.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存