用pageInfo对List手工分页
因业务需要,需对List,进行分页。
List来源,多种多样,有时不能用数据库发送sql来进行limit的分页
思路:
1.对List数组来源,进行subList的截取,类似于数据库发送limit。
2.构建一个pageinfo的工具类。
List<Description> descriptionByCategoryList = descriptionService.getDescriptionByCategoryList(20); int fromIndex = 0; int toIndex = 0; int pageSize = 20; int pageIndex = pn; int total = descriptionByCategoryList.size(); // 取出总记录数 if (total / pageSize == 0 && total % pageSize > 0) { fromIndex = 0; toIndex = total; } else { if (total / pageSize >= 1 && total % pageSize >= 0) { fromIndex = pageSize * (pageIndex - 1); if (pageSize * pageIndex >= total) { toIndex = total; } else { toIndex = pageSize * pageIndex; } } }
//做pn越界处理
try {
descriptionByCategoryList =descriptionByCategoryList.subList(fromIndex, toIndex);
} catch (IndexOutOfBoundsException e) {
fromIndex = 0;
toIndex= pageSize;
descriptionByCategoryList =descriptionByCategoryList.subList(fromIndex, toIndex);
}catch(IllegalArgumentException e) {
fromIndex = total-pageSize;
toIndex =total;
descriptionByCategoryList =descriptionByCategoryList.subList(fromIndex, toIndex);
}
descriptionByCategoryList = descriptionByCategoryList.subList(fromIndex, toIndex); //进行分页 PageInfo<Description> page = new PageInfo<Description>(descriptionByCategoryList, 7); page = PageHelperTool.initPageInfoObj(pageIndex, total, pageSize, page); //调用分页工具 mv.addObject("pageinfo", page);
分页工具类
package cn.taotao.utils; import com.github.pagehelper.PageInfo; /** * @Title: PageHelperTool.java * @Package * @Description: 分页工具类 */ public class PageHelperTool { /** * PageHelper中,如果当前页是最后一页,则返回的nextPage是0,即首页,而有时我们需要最后一页的nextPage是lastPage,因此写此方法 * * @param currentPage * @param pageObj * @return */ public static <T> PageInfo<T> lastPageSetNextPage(int currentPage, PageInfo<T> pageObj) { if (currentPage == pageObj.getPages()) { pageObj.setNextPage(pageObj.getPages()); } return pageObj; } public static <T> PageInfo<T> initPageInfoObj(int currentPage, int total, int pageSize, PageInfo<T> pageInfo) { pageInfo.setNextPage(currentPage < ((total + pageSize - 1) / pageSize) ? currentPage + 1 : currentPage); pageInfo.setTotal(total); pageInfo.setPageNum(currentPage); pageInfo.setPages((total + pageSize - 1) / pageSize); pageInfo.setLastPage((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); return calcNavigatepageNums(pageInfo); } private static <T> PageInfo<T> calcNavigatepageNums(PageInfo<T> pageInfo) { // 当总页数小于或等于导航页码数时 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++; } } } return pageInfo; } }
题外另外,需要在排序的类,重写自定义compare的规则。就是说,让他知道如何排序