Mybatis-Plus 多表查询分页
1. 在mapper里面定义方法
/* 第一种写法
@Select("select c.* from tb_staff_customer_relation as scr left join tb_customer as c on scr.customer_id = c.id ${ew.customSqlSegment}")
List<Customer> getListById(Page<Customer> page, @Param(Constants.WRAPPER) Wrapper wrapper);
*/
/**
* 第二种写法
*/
@Select("select c.* from tb_staff_customer_relation as scr left join tb_customer as c on scr.customer_id = c.id ${ew.customSqlSegment}")
IPage<Customer> getListById(Page page, @Param(Constants.WRAPPER) Wrapper wrapper);
解释:
- page参数 com.baomidou.mybatisplus.extension.plugins.pagination.Page 需要导入此包
- wrapper参数 com.baomidou.mybatisplus.core.conditions.Wrapper 需要导入此包
- 固定写法,至于怎么生效的,我也不知道呢
2. service调用mapper里面的方法
/**
* 第二种写法,不使用page类 返回结果使用ipage
*/
@Override
public List<CustomerResponseVo> getListByStaffId(int staff_id, int page, int limit) {
QueryWrapper<Customer> wrapper = new QueryWrapper<>();
// 查询条件
wrapper.eq("scr.staff_id", staff_id);
// 结果集排序
wrapper.orderByDesc("c.id");
IPage<Customer> customerIPage = staffCustomerRelationMapper.getListById(new Page(page, limit), wrapper);
ArrayList<CustomerResponseVo> arrayList = new ArrayList<>();
// 获取结果集
List<Customer> list = customerIPage.getRecords();
// 拼装返回数据
for (Customer customer : list) {
CustomerResponseVo responseVo = GemBeanUtils.copyProperties(customer, CustomerResponseVo.class);
arrayList.add(responseVo);
}
return arrayList;
}
java初学人员记录笔记,欢迎大佬指教
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。