当list的集合来自多个数据源的时候,所使用的PageHelper就不适合分页查询了,必须要手动分页。那么怎么进行手动分页呢?以下是封装的工具类
public class PageInfoTool {
public static <T> PageResult<T> initPageInfoObj(int currentPage, int total, int pageSize, PageResult<T> pageInfo,List<T> list) {
pageInfo.setNextPage(currentPage < ((total + pageSize - 1) / pageSize) ? currentPage + 1 : currentPage);
pageInfo.setTotal(total);
pageInfo.setPageSize(pageSize);
pageInfo.setPageNum(currentPage);
pageInfo.setPages((total + pageSize - 1) / pageSize);
pageInfo.setPrePage(currentPage > 1 ? currentPage - 1 : currentPage);
pageInfo.setIsFirstPage(currentPage == 1 ? true : false);
pageInfo.setIsLastPage(currentPage == (total + pageSize - 1) / pageSize ? true : false);
pageInfo.setHasPreviousPage(currentPage == 1 ? false : true);
pageInfo.setHasNextPage(currentPage == (total + pageSize - 1) / pageSize ? false : true);
pageInfo.setNavigatePages((total + pageSize - 1) / pageSize);
int start = 0;
int end = 0;
if(pageInfo.getPageNum() == 1){
start = 0;
pageInfo.setStartRow(0);
pageInfo.setHasPreviousPage(false);
pageInfo.setPrePage(0);
pageInfo.setIsFirstPage(true);
}else{
start = pageInfo.getPageSize()*(pageInfo.getPageNum()-1);
pageInfo.setStartRow(pageInfo.getPageSize()*(pageInfo.getPageNum()-1));
pageInfo.setHasPreviousPage(true);
pageInfo.setPrePage(pageInfo.getPageNum()-1);
pageInfo.setIsFirstPage(false);
}
if((pageInfo.getStartRow()+pageInfo.getPageSize() > pageInfo.getTotal())){
end = (int) pageInfo.getTotal();
pageInfo.setEndRow((int) pageInfo.getTotal());
pageInfo.setHasNextPage(false);
pageInfo.setIsLastPage(true);
}else {
end = start + pageInfo.getPageSize();
pageInfo.setEndRow(pageInfo.getStartRow() + pageInfo.getPageSize());
pageInfo.setHasNextPage(true);
pageInfo.setNextPage(pageInfo.getPageNum() + 1);
pageInfo.setIsLastPage(false);
pageInfo.setEndRow((pageInfo.getPageNum())*pageSize);
}
return calcNavigatepageNums(pageInfo,start,end,list);
}
private static <T> PageResult<T> calcNavigatepageNums(PageResult<T> pageInfo,int start,int end,List<T> list) {
//当总页数小于或等于导航页码数时
if (pageInfo.getPages() <= pageInfo.getNavigatePages()) {
pageInfo.setNavigatepageNums(new int[pageInfo.getPages()]);
for (int i = 0; i < pageInfo.getPages(); i++) {
pageInfo.getNavigatepageNums()[i] = i + 1;
}
} else { //当总页数大于导航页码数时
pageInfo.setNavigatepageNums(new int[pageInfo.getNavigatePages()]);
int startNum = pageInfo.getPageNum() - pageInfo.getNavigatePages() / 2;
int endNum = pageInfo.getPageNum() + pageInfo.getNavigatePages() / 2;
if (startNum < 1) {
startNum = 1;
//(最前navigatePages页
for (int i = 0; i < pageInfo.getNavigatePages(); i++) {
pageInfo.getNavigatepageNums()[i] = startNum++;
}
} else if (endNum > pageInfo.getPages()) {
endNum = pageInfo.getPages();
//最后navigatePages页
for (int i = pageInfo.getNavigatePages() - 1; i >= 0; i--) {
pageInfo.getNavigatepageNums()[i] = endNum--;
}
} else {
//所有中间页
for (int i = 0; i < pageInfo.getNavigatePages(); i++) {
pageInfo.getNavigatepageNums()[i] = startNum++;
}
}
}
if (start < end && end <= pageInfo.getTotal()){
pageInfo.setList(list.subList(start,end));
}
if(pageInfo.getSize() == 0) {
pageInfo.setStartRow(0);
pageInfo.setEndRow(0);
} else {
pageInfo.setStartRow(pageInfo.getStartRow() + 1);
pageInfo.setEndRow(pageInfo.getStartRow()-1+pageInfo.getPageSize());
}
return pageInfo;
}
}
需要手动分页时,代码中不要使用以下方法:
PageHelper.startPage(params.getPage(),params.getPageSize(),"ID DESC");
将查询的结果集都丢入到一个List中
List<T> list= new ArrayList<>();
for (int i = 0; i <= dataSourceList.size(); i++) {
if(i == 1){
continue;
}
DataSourceTypeUtil.callDatabase(i);
List<T> tempList= dao.list(params);
list.addAll(tempList);
}
然后将list丢入pageInfo最后执行PageInfoTool中的initPageInfoObj方法
PageResult<T> pageInfo = new PageResult<>(sumDataList);
pageInfo = PageInfoTool.initPageInfoObj(params.getPage(),list.size(),params.getPageSize(),pageInfo,list);
这样就实现了手动分页,我的工具类返回的PageResult是经过了封装的,可以根据你们的需求去改造。