mysqljson类型数据映射_如何将数据库中json格式的列值映射
到java对象的属性中
前⾔
mysql5.7版本之后,列值的类型⽀持json格式,那么如何将json格式的字段类型的值映射到java对象当中呢?以下记录⼀下转换⽅法.
数据库展⽰:
specs列为json格式,现将此列的值映射到java的对象的属性当中
分析json串,需要创建⼀个VO对象,⽤于保存json串中的对象
public class Spec implements Serializable {
private Integer key_id;
private String key;
private Integer value_id;
json值的类型有哪些private String value;
}
创建Pojo对象
@Entity
@Table(name = "sku")
@Getter
@Setter
public class SkuEntity extends BaseEntity {
@Id
private int id;
private BigDecimal price;
private BigDecimal discountPrice;
private byte online;
private String img;
private String title;
private int spuId;
//private Listspecs;
private String specs;
private String code;
private int stock;
private Integer categoryId;
private Integer rootCategoryId;
GenericAndJson⼯具类代码:
sevencell.api.utils;
import com.JsonProcessingException; import com.ype.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
ption.http.ServerErrorException; slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;
import java.io.IOException;
/**
* @Description: 此⼯具类进⾏json的序列化和反序列化
* @Author: my
* @CreateDate: 2020/3/5 22:08
* @UpdateUser:
* @UpdateDate: 2020/3/5 22:08
* @UpdateRemark: 修改内容
* @Version: 1.0
*/
@Component
@Slf4j
public class GenericAndJson {
private static ObjectMapper mapper;
/**
* 采⽤set⽅法⾃动注⼊
* @param mapper
*/
@Autowired
public void setMapper(ObjectMapper mapper) { GenericAndJson.mapper = mapper;
}
public static String objectToJson(T o){
try {
String jsonString = mapper.writeValueAsString(o);
return jsonString;
} catch (JsonProcessingException e) {
<(e.getMessage());
throw new ServerErrorException(9999);
}
}
public static T jsonToObject(String json, TypeReferencetypeReference){
try {
if (json.isEmpty()){
return null;
}
T t = adValue(json, typeReference);
return t;
} catch (IOException e) {
<(e.getMessage());
throw new ServerErrorException(9999);
}
}
}
⽅法⼀
1.我们将specs列的值当做String类型进⾏接收
private String specs;
2.编写specs的get/set⽅法,通过GenericAndJson⼯具类进⾏序列化和烦序列化public List getSpecs() {
if(this.specs.isEmpty()){
ptyList();
}
List specs = GenericAndJson.jsonToObject(this.specs, new TypeReference>() { });
return specs;
}
public void setSpecs(List specs) {
String json = GenericAndJson.objectToJson(specs);
this.specs = json;
}
测试结果:
{
"id": 1,
"price": 3999.00,
"discount_price": null,
"online": 1,
"img": "xxxxx",
"title": "复古双⾊沙发(藏青⾊)",
"spu_id": 1,
"specs": [
{
"key_id": 1,
"key": "颜⾊",
"value_id": 2,
"value": "藏青⾊"
},
{
"key_id": 7,
"key": "双⾊沙发尺⼨(⾮标)",
"value_id": 32,
"value": "1.5⽶ x 1⽶"
}
],
"code": "1$1-2#7-32",
"stock": 89,
"category_id": 35,
"root_category_id": null
}
⽅法⼆
1.新建⼯具类ConveterObjectAndJson,实现AttributeConverter接⼝sevencell.api.utils;
import com.JsonProcessingException; import com.ype.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
ption.http.ServerErrorException; slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import javax.persistence.AttributeCo
nverter;
import java.io.IOException;
/**
* @Description: 实现json的序列化和反序列化,只需要对需要转换的
* 属性打上@convet主机即可
* @Author: my
* @CreateDate: 2020/3/6 9:32
* @UpdateUser:
* @UpdateDate: 2020/3/6 9:32
* @UpdateRemark: 修改内容
* @Version: 1.0
*/
@Slf4j
public class ConveterObjectAndJson implements AttributeConverter { @Autowired
private ObjectMapper objectMapper;
@Override
public String convertToDatabaseColumn(T t) {
try {
String jsonString = objectMapper.writeValueAsString(t);
return jsonString;
} catch (JsonProcessingException e) {
<(e.getMessage());
throw new ServerErrorException(9999);
}
}
@Override
public T convertToEntityAttribute(String s) {
try {
if(s.isEmpty()){
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论