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