数据库映射为实体类
有一些数据想要映射为实体类是比较麻烦的,特别是一些存储在数据库里的 JSON 数据,如何查询处理该类数据呢?
1. 数据库JSON数据展示
== 现有如下类型的 JSON 数据存储在数据库里 ==
数据库中的数据:
区域坐标:List<List<Coordinate>> pointsArea
[
[{"latitude":31.5330733903248,"longitude":120.290795214727},{"latitude":31.5324866150653,"longitude":120.291653693163}],
[{"latitude":31.5309520811737,"longitude":120.291427281644},{"latitude":31.5303686190166,"longitude":120.291151310285}],
[{"latitude":31.5303686190166,"longitude":120.291151310285},{"latitude":31.5303686190166,"longitude":120.291151310285}]
]
=================================================================================================================================================
路段坐标:List<Coordinate> pointsRoad
[
{"latitude": 31.50184166903764, "longitude": 120.34707069396974}, {"latitude": 31.499607874509053, "longitude": 120.34785389900208},
{"latitude": 31.488777200863705, "longitude": 120.3506112098694}, {"latitude": 31.48862645725139, "longitude": 120.3506112098694},
{"latitude": 31.48846155139795, "longitude": 120.3506809473038}, {"latitude": 31.487921760889336, "longitude": 120.35077750682832},
{"latitude": 31.487656442554535, "longitude": 120.35082042217256}, {"latitude": 31.487381222076483, "longitude": 120.35080432891849}
]
==================================================================================================================================================
地图点位:List<Long> pointsId
[31, 32, 33, 34, 35, 36]
==================================================================================================================================================
保障区域(行政区):List<Integer> belongRegions
[0, 1, 2, 3, 4, 5, 6]
分别映射到下面的参数中
2. JSON数据映射到实体类
注:autoResultMap = true 一定要添加,否则无法成功映射
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName(value = "guarantee_task" , autoResultMap = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class GuaranteeTask extends BaseEntity {
private static final long serialVersionUID = 1L;
/** 保障区域(行政区) */
@TableField(typeHandler = JacksonTypeHandler.class)
private List<Integer> belongRegions;
/** 地图点位 id */
@TableField(typeHandler = ListInteger2ListLongTypeHandler.class)
private List<Long> pointsId;
/** 保障等级*/
private Integer taskLevel;
/** 区域坐标 */
@TableField(typeHandler = JacksonTypeHandler.class)
private List<List<Coordinate>> pointsArea;
/** 路段坐标 */
@TableField(typeHandler = JacksonTypeHandler.class)
private List<Coordinate> pointsRoad;
}
- Coordinate映射的实体类
/**
* @author Memo
* @version 1.0
* @description: 坐标点
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Coordinate {
private BigDecimal latitude;