前言
需求:多表查询数据,使用mybatisplus实现一对一关系和一对多关系的映射
一、需求
查询订单的详情信息,需要展示车源表信息和车源照片表信息。
二、实现
1.实现分析
1)通过给原生实体类加上实体属性,然后通过赋值到对应的实体属性就行。(不用写原生的xml映射方式)
2.代码实现
entity:
// @TableName("order")
// @ApiModel(description = "xx表")
@Data
public class CarSourceOrder implements Serializable {
private static final long serialVersionUID = 1L;
@TableField(exist = false)
private CarSource carSource;
@TableField(exist = false)
private List<CarSourceFile> carSourceFileList;
}
ServiceImpl:
@Override
public CarSourceOrder findByCarSourceOrderInfo(Integer id) {
CarSourceOrder carSourceOrder = carSourceOrderDao.selectById(id);
// 判断,以防carSourceOrder.getCarSourceId() 空指针异常
if (StringUtils.isEmpty(carSourceOrder)){
return carSourceOrder;
}
Integer carSourceId = carSourceOrder.getCarSourceId();
carSourceOrder.setCarSource(carSourceDao.selectById(carSourceId));
carSourceOrder.setCarSourceFileList(carSourceFileDao.selectList(new EntityWrapper<CarSourceFile>().eq("car_source_id", carSourceId)));
return carSourceOrder;
}
2.返回格式
代码如下(示例):
{
"msg": "执行成功",
"code": 0,
"data": {
"id": 5,
"orderNumber": "123456789012345678"
"yhcsCarSource": {
"carModelName": "2014款 奔驰E级 E260L 豪华型",
"carPlace": "广东"
},
"yhcsCarSourceFileList": [
{
"carSourceId": 40,
"id": 6,
"url": "https://xxx.jpg"
},
{
"carSourceId": 40,
"id": 7,
"url": "https://xxx.jpg"
}
]
}
}
总结
1,VO和DTO不能使用,使用就不能调mybatis-plus提供的方法,不够快捷。
2,(补充)同理,对某个忽略字段进行分页处理也可以。例如: @TableField(exist = false)
private PageUtils carSourceList;
@TableField(exist = false)
private List<Map<String,String>> phones;