从一段优秀的代码开始..........
public Result selectGroupRef(Context context) { //获取任务编码 String taskCode = context.getString("taskCode"); PageBounds pageBounds = ContextUtils.getPageBounds(context); // 获取组的列表 PageList<TaskGroupRef> taskGroupRefList = taskGroupRefService.selectGroupRef(taskCode, pageBounds); return Result.success(new PageVo<>(taskGroupRefList), "查询成功!"); }
细致的理解代码:
一、PageBounds
PageBounds pageBounds = ContextUtils.getPageBounds(context);
后面的代码都要加上 pageBounds ,这样可以一系列的传递过去,实现分页效果,一定要加上。
优化好了的分页代码:
public class PageBounds extends RowBounds implements Serializable { private static final long serialVersionUID = -6414350656252331011L; public static final int NO_PAGE = 1; protected int page; protected int limit; protected List<Order> orders; protected boolean containsTotalCount; protected Boolean asyncTotalCount; public PageBounds() { this.page = 1; this.limit = 2147483647; this.orders = new ArrayList(); this.containsTotalCount = false; } public PageBounds(RowBounds rowBounds) { this.page = 1; this.limit = 2147483647; this.orders = new ArrayList(); if (rowBounds instanceof PageBounds) { PageBounds pageBounds = (PageBounds)rowBounds; this.page = pageBounds.page; this.limit = pageBounds.limit; this.orders = pageBounds.orders; this.containsTotalCount = pageBounds.containsTotalCount; this.asyncTotalCount = pageBounds.asyncTotalCount; } else { this.page = rowBounds.getOffset() / rowBounds.getLimit() + 1; this.limit = rowBounds.getLimit(); } } public PageBounds(int limit) { this.page = 1; this.limit = 2147483647; this.orders = new ArrayList(); this.limit = limit; this.containsTotalCount = false; } public PageBounds(int page, int limit) { this(page, limit, new ArrayList(), true); } public PageBounds(int page, int limit, boolean containsTotalCount) { this(page, limit, new ArrayList(), containsTotalCount); } public PageBounds(List<Order> orders) { this(1, 2147483647, orders, false); } public PageBounds(Order... order) { this(1, 2147483647, (Order[])order); this.containsTotalCount = false; } public PageBounds(int page, int limit, Order... order) { this(page, limit, Arrays.asList(order), true); } public PageBounds(int page, int limit, List<Order> orders) { this(page, limit, orders, true); } public PageBounds(int page, int limit, List<Order> orders, boolean containsTotalCount) { this.page = 1; this.limit = 2147483647; this.orders = new ArrayList(); this.page = page; this.limit = limit; this.orders = orders; this.containsTotalCount = containsTotalCount; } public int getPage() { return this.page; } public void setPage(int page) { this.page = page; } public int getLimit() { return this.limit; } public void setLimit(int limit) { this.limit = limit; } public boolean isContainsTotalCount() { return this.containsTotalCount; } public void setContainsTotalCount(boolean containsTotalCount) { this.containsTotalCount = containsTotalCount; } public List<Order> getOrders() { return this.orders; } public void setOrders(List<Order> orders) { this.orders = orders; } public Boolean getAsyncTotalCount() { return this.asyncTotalCount; } public void setAsyncTotalCount(Boolean asyncTotalCount) { this.asyncTotalCount = asyncTotalCount; } public int getOffset() { return this.page >= 1 ? (this.page - 1) * this.limit : 0; } public String toString() { StringBuilder sb = new StringBuilder("PageBounds{"); sb.append("page=").append(this.page); sb.append(", limit=").append(this.limit); sb.append(", orders=").append(this.orders); sb.append(", containsTotalCount=").append(this.containsTotalCount); sb.append(", asyncTotalCount=").append(this.asyncTotalCount); sb.append('}'); return sb.toString(); } }
二、new PageVo<>
return Result.success(new PageVo<>(taskGroupRefList), "查询成功!");
不要忘了这个 new PageVo<> ,如果不加后台没有分页显示效果。