业务场景:
选择机构,查看机构下对应的部门,包括子机构的部门。
正常业务场景下,在service层只做一次查询就能满足需求,若需要分页,只需在Controller层添加继承BaseController,使用startPage()和getDataTable()即可。
Controller层:
@RestController
@RequestMapping("/system/dept")
public class DeptController extends BaseController {
@Resource
private IDeptService deptService;
/**
* 查询部门列表
*/
@GetMapping("/list")
public TableDataInfo list(Dept dept){
startPage();
List<Dept> list = dpetService.selectDeptList(dept);
return getDataTable(list);
}
}
Service层:
/**
* 查询部门列表
*/
@Override
public List<Dept> selectDeptList(Dept dept){
return deptMapper.selectDeptList(dept);
}
分页工具只能在数据查询出来不做任何处理的情况下使用,因为这个时候列表的总数是固定的,分页的参数可控。
但按照需求,我需要先查出当前机构及其子机构,然后通过机构再查询出对应部门。所以,我在service层做了数据处理并添加到list集合中返回,分页失效。
List<Dept> list = new ArrayList<>();
List<Institution> institutionList = institutionMapper.queryInstitutionList(institution);
if (!CollectionUtils.isEmpty(institutionList)){
for (Institution institution : institutionList) {
List<Dept> deptList = deptMapper.selectDeptAll(institution.getInstitutionId());
if (!CollectionUtils.isEmpty(deptList)){
for (Dept d : deptList) {
Dept dept = new Dept();
dept.setOrgName(d.getOrgName());
dept.setCount(d.getCount());
dept.setId(d.getId());
list.add(dept);
}
}
}
}
return list;
解决方法(参考大佬的解决方法):
方式一:
PS:pageNum和pageSize是前端传进来的分页对象的属性
@GetMapping("/queryAll")
public TableDataInfo queryAll(@RequestParam(value = "institutionId",required = false) Long institutionId){
PageDomain pageDomain = TableSupport.buildPageRequest();
Integer pageNum = pageDomain.getPageNum();
Integer pageSize = pageDomain.getPageSize();
List<Dept> list = new ArrayList<>();
Institution institution = new Institution();
institution.setInstitutionId(institutionId);
List<Institution> institutionList = institutionService.queryInstitutionList(institution);
if (!CollectionUtils.isEmpty(institutionList)){
for (Institution i : institutionList) {
List<Dept> deptList = deptService.selectDeptAll(i.getInstitutionId());
if (!CollectionUtils.isEmpty(deptList)){
for (Dept d : deptList) {
Dept dept = new Dept();
dept.setOrgName(d.getOrgName());
dept.setCount(d.getCount());
dept.setId(d.getId());
list.add(dept);
}
}
}
}
//获取处理好的list集合
int num = list.size();
list = list.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
TableDataInfo rspData = new TableDataInfo();
rspData.setCode(HttpStatus.SUCCESS);
rspData.setRows(list);
rspData.setTotal(num);
return rspData;
}
方式二:
改造若依BaseController通用数据处理类中getDataTable方法。
原本getDataTable方法:
/**
* 响应请求分页数据
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected TableDataInfo getDataTable(List<?> list)
{
TableDataInfo rspData = new TableDataInfo();
rspData.setCode(HttpStatus.SUCCESS);
rspData.setMsg("查询成功");
rspData.setRows(list);
rspData.setTotal(new PageInfo(list).getTotal());
return rspData;
}
修改后getDataTable方法:
/**
* 响应请求分页数据
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected TableDataInfo getDataTable(List<?> listVO, List<?> list)
{
TableDataInfo rspData = new TableDataInfo();
rspData.setCode(HttpStatus.SUCCESS);
rspData.setMsg("查询成功");
rspData.setRows(listVO);
rspData.setTotal(new PageInfo(list).getTotal());
return rspData;
}
这里直接上大佬的贴图:
😁