前言
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;
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工具类代码:
package com.my.sevencell.api.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper