使用PageInfo把list手动进行分页处理
手动分页需要手动设置PageInfo的各个参数,指定分页规则。下面就是一个将list列表手动分页成PageInfo的工具类:
package utils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.LinkedList;
import java.util.List;
public class PageInfoUtils {
public static <T> PageInfo<T> list2PageInfo(List<T> arrayList, Integer pageNum, Integer pageSize) {
//实现list分页
PageHelper.startPage(pageNum, pageSize);
int pageStart = pageNum == 1 ? 0 : (pageNum - 1) * pageSize;
int pageEnd = arrayList.size() < pageSize * pageNum ? arrayList.size() : pageSize * pageNum;
List<T> pageResult = new LinkedList<T>();
if (arrayList.size() > pageStart) {
pageResult = arrayList.subList(pageStart, pageEnd);
}
PageInfo<T> pageInfo = new PageInfo<T>(pageResult);
//获取PageInfo其他参数
pageInfo.setTotal(arrayList.size());
int endRow = pageInfo.getEndRow() == 0 ? 0 : (pageNum - 1) * pageSize + pageInfo.getEndRow() + 1;
pageInfo.setEndRow(endRow);
boolean hasNextPage = arrayList.size() <= pageSize * pageNum ? false : true;
pageInfo.setHasNextPage(hasNextPage);
boolean hasPreviousPage = pageNum == 1 ? false : true;
pageInfo.setHasPreviousPage(hasPreviousPage);
pageInfo.setIsFirstPage(!hasPreviousPage);
boolean isLastPage = (arrayList.size() > pageSize * (pageNum - 1) && arrayList.size() <= pageSize * pageNum) ? true : false;
pageInfo.setIsLastPage(isLastPage);
int pages = arrayList.size() % pageSize == 0 ? arrayList.size() / pageSize : (arrayList.size() / pageSize) + 1;
pageInfo.setNavigateLastPage(pages);
int[] navigatePageNums = new int[pages];
for (int i = 1; i < pages; i++) {
navigatePageNums[i - 1] = i;
}
pageInfo.setNavigatepageNums(navigatePageNums);
int nextPage = pageNum < pages ? pageNum + 1 : 0;
pageInfo.setNextPage(nextPage);
pageInfo.setPageNum(pageNum);
pageInfo.setPageSize(pageSize);
pageInfo.setPages(pages);
pageInfo.setPrePage(pageNum - 1);
pageInfo.setSize(pageInfo.getList().size());
int starRow = arrayList.size() < pageSize * pageNum ? 1 + pageSize * (pageNum - 1) : 0;
pageInfo.setStartRow(starRow);
return pageInfo;
}
}