若依框架-对数据二次处理后分页失效

业务场景:

选择机构,查看机构下对应的部门,包括子机构的部门。

正常业务场景下,在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;
    }

这里直接上大佬的贴图:

😁

  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值