spring data jpa + mysql使用json 类型

  1. 引入依赖
<dependency>
   <groupId>com.vladmihalcea</groupId>
   <artifactId>hibernate-types-52</artifactId>
   <version>2.4.3</version>
</dependency>
  1. 定义实体类
@Data
@Entity
@TypeDef(name = "json", typeClass = JsonStringType.class)
public class ExpressOrder{
    /**主键自增 */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
 
    /**商品相关信息 */
    @Type(type = "json")
    @Column(columnDefinition = "json" )
    private List<CargoModel> cargoModelList;
 
    /**增值服务信息 */
    @Type(type = "json")
    @Column(columnDefinition = "json" )
    private List<AddedServiceModel> addedServiceModelList;
}
 
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CargoModel implements Serializable {
    private String name;// 货物名称
    private String count;// 货物数量
}
 
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AddedServiceModel implements Serializable {
    private String name;//增值服务名,如COD等。
    private String value;
    private String value1;
}

说明:addedServiceModelList 和cargoModelList 分别表示 表中字段added_service_model_list 和 cargo_model_list,其类型均为json

posted @ 2020-04-09 14:17  尘世的流浪人  阅读(10197)  评论(0编辑  收藏  举报