通过先前的文章:【Spring Boot】MyBatis引入 分页插件pagehelper
知道如何使用分页插件pagehelper,现在对这个分页插件再简单包装一下,使它使用起来更方便,自带分页条:
1、分页条类(输出分页字符串:首页、上一页、下一页、尾页):
package com.wenjianbao.codepub.model.page; import cn.hutool.core.net.url.UrlBuilder; import cn.hutool.core.net.url.UrlQuery; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.NumberUtil; import com.wenjianbao.codepub.helper.PageURLHelper; import java.util.Map; /** * 分页条类(输出分页字符串:首页、上一页、下一页、尾页) */ public class Pagination { private int pageNum = 1; // 【必填】当前分页数 private int pageSize = 10; // 【必填】每页记录数 private int totalPages = 0; // 【必填】总共分页数 private long totalSize = 0; // 【必填】总记录数 private int[] pageSizeOption = {10, 20, 30, 40, 50}; // 每页记录数 下拉选择项 private int maxPages = 0; // 最大能显示的分页数(比如说,本来总共有250页,但是想让它只显示前50页) private int columnPages = 5; // 分页栏显示的页数 private String basePageUrl = ""; /** * 构造方法 */ public Pagination() { init(); } /** * 初始化 */ private void init() { // 基础的URL构造器 UrlBuilder urlBuilder = UrlBuilder.of(PageURLHelper.getPageUrl()); Map<CharSequence, CharSequence> urlParams = urlBuilder.getQuery().getQueryMap(); UrlQuery urlQuery = new UrlQuery(); String[] deleteParams = {"pageNum", "pageSize"}; for (Map.Entry<CharSequence, CharSequence> entry : urlParams.entrySet()) { if (!ArrayUtil.contains(deleteParams, entry.getKey())) { urlQuery.add(entry.getKey(), entry.getValue()); } } basePageUrl = urlBuilder.setQuery(urlQuery).toString(); // 总页数 if (maxPages > 0 && maxPages < totalPages) { totalPages = maxPages; } // 校正当前页,不能越界 pageNum = NumberUtil.max(pageNum, 1); pageNum = NumberUtil.min(pageNum, totalPages); } private String makeUrl(int pageNum) { UrlBuilder urlBuilder = UrlBuilder.ofHttp(basePageUrl); urlBuilder.addQuery("pageNum", String.valueOf(pageNum)); return urlBuilder.toString(); } private String makeUrl(int pageNum, int pageSize) { UrlBuilder urlBuilder = UrlBuilder.ofHttp(basePageUrl); urlBuilder.addQuery("pageNum", String.valueOf(pageNum)); urlBuilder.addQuery("pageSize", String.valueOf(pageSize)); return urlBuilder.toString(); } /** * 获取分页字符串 */ public String getPageStr() { StringBuilder pageSb = new StringBuilder(); int offset = NumberUtil.ceilDiv(columnPages, 2) - 1; int from = 0; int to = 0; if (totalPages < columnPages) { from = 1; to = totalPages; } else { from = pageNum - offset; to = from + columnPages - 1; if (from < 1) { from = 1; to = columnPages; } else if (to > totalPages) { from = totalPages - columnPages + 1; to = totalPages; } } // 首页 if (from > 1) { pageSb.append("<li><a href=\"") .append(makeUrl(1, pageSize)) .append("\" class=\"first\">首页</a></li>"); } // 上一页 if (pageNum > 1) { pageSb.append("<li><a href=\"") .append(makeUrl(pageNum - 1, pageSize)) .append("\" class=\"prev\">上一页</a></li>"); } // 中间数字页 for (int i = from; i <= to; i++) { if (i == pageNum) { pageSb.append("<li class=\"active\"><a>") .append(i) .append("</a></li>"); } else { pageSb.append("<li><a href=\"") .append(makeUrl(i, pageSize)) .append("\">") .append(i) .append("</a></li>"); } } // 下一页 if (pageNum < totalPages) { pageSb.append("<li><a href=\"") .append(makeUrl(pageNum + 1, pageSize)) .append("\" class=\"next\">下一页</a></li>"); } // 末页 if (to < totalPages) { pageSb.append("<li><a href=\"") .append(makeUrl(totalPages, pageSize)) .append("\" class=\"last\">末页</a></li>"); } // 每页记录数 下拉框 StringBuilder pageSizeSb = new StringBuilder(); pageSizeSb.append("<li>总共<strong>") .append(totalSize) .append("</strong>条记录,每页显示<select #onchange#>#option#</select>条记录 </li>"); // 下拉框 onChange事件 StringBuilder onchangeSb = new StringBuilder(); onchangeSb.append(" onchange=\"location.href='") .append(makeUrl(1)) .append("&pageSize=' + this.value\" "); StringBuilder optionSb = new StringBuilder(); for (int size : pageSizeOption) { String selectedStr = ""; if (pageSize == size) { selectedStr = " selected=\"selected\" "; } optionSb.append("<option value=\"") .append(size) .append("\" ") .append(selectedStr) .append(">") .append(size) .append("</option>") ; } String pageSizeStr = pageSizeSb.toString() .replace("#onchange#", onchangeSb.toString()) .replace("#option#", optionSb.toString()); return "<ul class=\"pagination clearfix\">" + pageSizeStr + pageSb.toString() + "</ul>"; } public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalPages() { return totalPages; } public void setTotalPages(int totalPages) { this.totalPages = totalPages; } public long getTotalSize() { return totalSize; } public void setTotalSize(long totalSize) { this.totalSize = totalSize; } public int[] getPageSizeOption() { return pageSizeOption; } public void setPageSizeOption(int[] pageSizeOption) { this.pageSizeOption = pageSizeOption; } public int getMaxPages() { return maxPages; } public void setMaxPages(int maxPages) { this.maxPages = maxPages; } public int getColumnPages() { return columnPages; } public void setColumnPages(int columnPages) { this.columnPages = columnPages; } }
2、分页查询结果封装类
package com.wenjianbao.codepub.model.page; import com.github.pagehelper.PageInfo; import java.util.List; /** * 分页查询结果封装类 */ public class PageResult<T> { private final PageInfo<T> pageInfo; private final Pagination pagination; /** * 构造方法 */ public PageResult(List<T> list) { this.pageInfo = new PageInfo<>(list); this.pagination = new Pagination(); this.pagination.setPageNum(getPageNum()); this.pagination.setPageSize(getPageSize()); this.pagination.setTotalPages(getTotalPages()); this.pagination.setTotalSize(getTotalSize()); } /** * 快捷生成分页结果类 */ public static <T> PageResult<T> of(List<T> list) { return new PageResult<>(list); } /** * 当前页分页数 */ public int getPageNum() { return this.pageInfo.getPageNum(); } /** * 每页记录数 */ public int getPageSize() { return this.pageInfo.getPageSize(); } /** * 总记录数 */ public long getTotalSize() { return this.pageInfo.getTotal(); } /** * 总共分页数 */ public int getTotalPages() { return this.pageInfo.getPages(); } /** * 分页条 */ public String getPageStr() { return this.pagination.getPageStr(); } /** * 分页数据列表 */ public List<T> getList() { return this.pageInfo.getList(); } }
使用示例:
// 设置分页信息 PageHelper.startPage(pageNum, pageSize); // 分页查询博文 List<Blog> blogList = blogMapper.selectPage(); // 获取分页信息 PageResult<Blog> pageResult = PageResult.of(blogList); // 输出分页信息 System.out.println(pageResult.getPageNum()); // 当前页分页数 System.out.println(pageResult.getPageSize()); // 每页记录数 System.out.println(pageResult.getTotalSize()); // 总记录数 System.out.println(pageResult.getTotalPages()); // 总共分页数 System.out.println(pageResult.getPageStr()); // 分页条 System.out.println(pageResult.getList()); // 分页数据列表