项目开发中有时候由于查询的数据比较复杂,可能会对查询的到的数据进行一些处理,比如合并,或者筛选掉一部分的数据,那么就无法再查询的时候去进行分页了,而需要将处理之后的集合数据,进行手动分页处理,以下是手动处理分页的案例参考:
Page<返回的实体对象> resultPage = Mapper.selectData(param);//通过sql查询得到的数据//...中间对resultPage里的Records数据进行了处理,这里省略resultPage.setRecords(resultList);//然后新生成了一个集合对象resultList 把处理之后的数据重新赋值到Records里//手动根据以下几个条件对数据进行分页resultPage.setTotal(resultList.size());//重新对总条目赋值,因为数据改变了long current = param.getPageNo();//页面传入的当前页long pages = param.getPageSize();//页面传入的每页条目long total = resultPage.getTotal();//修改得到的总条数long startIndex=0;//初始条目long endIndex =0;//每页条目long indexPage = (current - 1)*pages;if(indexPage > total){startIndex = 0;}else{startIndex = indexPage;}long pageSize = current*pages;if(pageSize>total){endIndex = total;}else{endIndex = pageSize;}List<返回的实体对象> paginatedList = resultList.subList((int)startIndex,(int)endIndex);resultPage.setRecords(paginatedList);//分页之后重新赋值到返回的Records对象里return new PageResult<>(resultPage);//最后返回到页面