不需要分页查询sql,只用查询所有结果返回List集合,通过以下PageVO封装分页信息
package owp; import java.io.Serializable; import java.util.ArrayList; import java.util.List; /** * 不需要分页sql的分页 * @author Jeason * * @param <T> */ public class PageVO2<T> implements Serializable{ private static final long serialVersionUID = 4278419095873395253L; /** * 分页大小 */ private int pageSize; /** * 页码 */ private int pageIndex; /** * 所有数据集合 */ private List<T> allData; public PageVO2() { super(); } public PageVO2(int pageSize,int pageIndex,List<T> allData) { this.pageSize = pageSize; this.pageIndex = pageIndex; this.allData = allData; } public int getPageSize() { return this.pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getPageIndex() { return this.pageIndex; } public void setPageIndex(int pageIndex) { this.pageIndex = pageIndex; } public List<T> getAllData() { return this.allData; } public void setAllData(List<T> allData) { this.allData = allData; } /** * 获取分页数据集合 */ public List<T> getPageDate() { int fromIndex = (this.pageIndex-1) * this.pageSize; int toIndex = this.pageIndex * this.pageSize; toIndex = toIndex > getCountRow() ? getCountRow() : toIndex; return this.allData.subList(fromIndex, toIndex); } /** * 获取总行数 */ public int getCountRow() { return this.allData.size(); } /** * 获取总页数 */ public int getCountPage() { int row = getCountRow(); if (row % this.pageSize == 0) { return row / this.pageSize; } return row / this.pageSize + 1; } /** * 是否第一页 */ public boolean isFirst(){ return ((getCountRow() == 0) || (this.pageIndex <= 1)); } /** * 是否最后一页 */ public boolean isLast() { return ((getCountRow() == 0) || (this.pageIndex >= getCountPage())); } /** * 上一页 */ public int getPrevPage() { if (this.pageIndex <= 1) { return 1; } return this.pageIndex - 1; } /** * 下一页 */ public int getNextPage(){ if (this.pageIndex >= getCountPage()) { return getCountPage(); } return this.pageIndex + 1; } public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("1");list.add("2");list.add("3");list.add("4");list.add("5"); list.add("6");list.add("7");list.add("8");list.add("9"); list.add("10"); list.add("11"); list.add("12"); PageVO2<String> po = new PageVO2<String>(5, 3,list); System.out.println("总行数"+po.getCountRow()); System.out.println("总页数"+po.getCountPage()); System.out.println("上一页"+po.getPrevPage()); System.out.println("下一页"+po.getNextPage()); System.out.println("第一页吗?"+po.isFirst()); System.out.println("最后一页吗?"+po.isLast()); System.out.println(po.getPageDate()); } }
文章来源: https://blog.csdn.net/Jeason5/article/details/90474180