mysql存储json、json数据返回
1、数值类型的jsonpublic class CommonIntegerTypeHandler extends BaseAttributeTypeHandler<List<Integer>> {}2、字符串类型的jsonpublic class CommonStringTypeHandler extends BaseAttributeTypeHandler<List
·
1、修改数据库字段类型为json 部分低版本mysql不支持
2、定义基类处理器
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import io.lettuce.core.dynamic.support.ResolvableType;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.io.IOException;
import java.lang.reflect.Type;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* typeHandler 基类处理
*/
public abstract class BaseAttributeTypeHandler<T> extends BaseTypeHandler<Object> {
private JavaType javaType;
/**
* ObjectMapper
*/
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
/**
* 构造方法
*/
public BaseAttributeTypeHandler() {
ResolvableType resolvableType = ResolvableType.forClass(getClass());
Type type = resolvableType.as(BaseAttributeTypeHandler.class).getGeneric().getType();
javaType = constructType(type);
}
private static JavaType constructType(Type type) {
Assert.notNull(type, "[Assertion failed] - type is required; it must not be null");
return TypeFactory.defaultInstance().constructType(type);
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType)
throws SQLException {
ps.setString(i, JSONUtil.toJsonStr(parameter));
}
@Override
public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
String value = rs.getString(columnName);
return convertToEntityAttribute(value);
}
@Override
public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return convertToEntityAttribute(rs.getString(columnIndex));
}
@Override
public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
String value = cs.getString(columnIndex);
return convertToEntityAttribute(value);
}
private Object convertToEntityAttribute(String dbData) {
if (StrUtil.isEmpty(dbData)) {
if (List.class.isAssignableFrom(javaType.getRawClass())) {
return Collections.emptyList();
} else if (Set.class.isAssignableFrom(javaType.getRawClass())) {
return Collections.emptySet();
} else if (Map.class.isAssignableFrom(javaType.getRawClass())) {
return Collections.emptyMap();
} else {
return null;
}
}
return toObject(dbData, javaType);
}
private static <T> T toObject(String json, JavaType javaType) {
Assert.hasText(json, "[Assertion failed] - this json must have text; it must not be null, empty, or blank");
Assert.notNull(javaType, "[Assertion failed] - javaType is required; it must not be null");
try {
return OBJECT_MAPPER.readValue(json, javaType);
} catch (com.fasterxml.jackson.core.JsonParseException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (JsonMappingException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}
3、number类型的json
public class CommonIntegerTypeHandler extends BaseAttributeTypeHandler<List<Integer>> {
}
4、字符串类型的json
public class CommonStringTypeHandler extends BaseAttributeTypeHandler<List<String>> {
}
5、对象类型的json
public class CommonJsonImgTypeHandler extends BaseAttributeTypeHandler<List<Images>> {
}
6、实体类添加注解
!!! 一定要添加 否则不会起作用
@TableName(value = "t_xxx", autoResultMap = true)
//字符串格式
@TableField(typeHandler = CommonStringTypeHandler.class)
private List<String> bigImages;
//数值格式
@TableField(typeHandler = CommonIntegerTypeHandler.class)
private List<Integer> bigImages;
//对象格式
@TableField(typeHandler = CommonJsonImgTypeHandler.class)
private List<Images> detailsFigureImages;
注意mapper层记得修改字段类型
结果:
好了这样就可以愉快的存储json格式的数据了!
更多推荐
已为社区贡献2条内容
所有评论(0)