package com.utils; import java.io.Serializable; import java.util.List; import java.util.Map; import com.baomidou.mybatisplus.plugins.Page; /** * 分页工具类 * * 核心职责:封装分页查询结果,提供统一的分页数据格式 * * 功能特性: * 1. 封装MyBatis-Plus的Page对象,提供更友好的接口 * 2. 支持多种构造方式 * 3. 实现Serializable接口,支持序列化传输 * 4. 提供完整的分页信息(总数、页数、当前页、数据列表等) * * 使用场景: * - 控制器层返回分页数据 * - 服务层封装分页结果 * - 前后端分页数据交互 */ public class PageUtils implements Serializable { // 序列化版本ID private static final long serialVersionUID = 1L; // 总记录数 private long total; // 每页记录数 private int pageSize; // 总页数 private long totalPage; // 当前页数 private int currPage; // 列表数据 private List list; /** * 分页构造函数(基础版本) * 通过基本分页参数构建分页结果 * * @param list 列表数据 * @param totalCount 总记录数 * @param pageSize 每页记录数 * @param currPage 当前页数 */ public PageUtils(List list, int totalCount, int pageSize, int currPage) { this.list = list; this.total = totalCount; this.pageSize = pageSize; this.currPage = currPage; // 计算总页数:总记录数 / 每页记录数,向上取整 this.totalPage = (int) Math.ceil((double) totalCount / pageSize); } /** * 分页构造函数(MyBatis-Plus Page版本) * 通过MyBatis-Plus的Page对象构建分页结果 * * @param page MyBatis-Plus分页对象 */ public PageUtils(Page page) { this.list = page.getRecords(); // 获取分页数据列表 this.total = page.getTotal(); // 获取总记录数 this.pageSize = page.getSize(); // 获取每页大小 this.currPage = page.getCurrent(); // 获取当前页码 this.totalPage = page.getPages(); // 获取总页数 } /** * 分页构造函数(参数Map版本) * 通过请求参数构建空数据的分页结果 * * @param params 请求参数Map,包含分页参数 */ public PageUtils(Map params) { // 使用Query工具类解析参数创建Page对象 Page page = new Query(params).getPage(); // 委托给Page构造函数处理 new PageUtils(page); } // Getter 和 Setter 方法 /** * 获取每页记录数 * @return 每页记录数 */ public int getPageSize() { return pageSize; } /** * 设置每页记录数 * @param pageSize 每页记录数 */ public void setPageSize(int pageSize) { this.pageSize = pageSize; } /** * 获取当前页数 * @return 当前页数 */ public int getCurrPage() { return currPage; } /** * 设置当前页数 * @param currPage 当前页数 */ public void setCurrPage(int currPage) { this.currPage = currPage; } /** * 获取列表数据 * @return 数据列表 */ public List getList() { return list; } /** * 设置列表数据 * @param list 数据列表 */ public void setList(List list) { this.list = list; } /** * 获取总页数 * @return 总页数 */ public long getTotalPage() { return totalPage; } /** * 设置总页数 * @param totalPage 总页数 */ public void setTotalPage(long totalPage) { this.totalPage = totalPage; } /** * 获取总记录数 * @return 总记录数 */ public long getTotal() { return total; } /** * 设置总记录数 * @param total 总记录数 */ public void setTotal(long total) { this.total = total; } // 可以添加的实用方法: /** * 判断是否有上一页 * @return 是否有上一页 */ public boolean hasPrevious() { return currPage > 1; } /** * 判断是否有下一页 * @return 是否有下一页 */ public boolean hasNext() { return currPage < totalPage; } /** * 获取上一页页码 * @return 上一页页码,如果没有上一页返回-1 */ public int getPreviousPage() { return hasPrevious() ? currPage - 1 : -1; } /** * 获取下一页页码 * @return 下一页页码,如果没有下一页返回-1 */ public int getNextPage() { return hasNext() ? currPage + 1 : -1; } /** * 获取起始记录位置(用于SQL的LIMIT) * @return 起始记录位置 */ public int getStartIndex() { return (currPage - 1) * pageSize; } /** * 转换为Map格式,便于JSON序列化 * @return 分页数据的Map表示 */ public Map toMap() { return java.util.Map.of( "total", total, "pageSize", pageSize, "totalPage", totalPage, "currPage", currPage, "list", list, "hasPrevious", hasPrevious(), "hasNext", hasNext(), "previousPage", getPreviousPage(), "nextPage", getNextPage() ); } /** * 创建空分页结果 * @param pageSize 每页大小 * @param currPage 当前页码 * @return 空分页结果 */ public static PageUtils empty(int pageSize, int currPage) { return new PageUtils(java.util.Collections.emptyList(), 0, pageSize, currPage); } /** * 字符串表示 * @return 分页信息的字符串表示 */ @Override public String toString() { return "PageUtils{" + "total=" + total + ", pageSize=" + pageSize + ", totalPage=" + totalPage + ", currPage=" + currPage + ", list=" + (list != null ? list.size() : 0) + " items" + '}'; } }