数据库中的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中引用这个文件
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 JsonTypeHandlerextends 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); } }
- 映射中 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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)