java分页工具类
分页工具类被常常用到,大多数分页工具类都是大同小异,自己也写一个通用分页工具类,给入门者铺路,工具类仅供参考,可按你自己的业务修改。大神莫要吐槽,如有不当之处请指正。
文件:PageUtil.java
package com.util; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; import java.util.List; /** * 分页工具(含页码) * Created by cxq on 2017/11/22. */ public class PageUtil<T> { /**显示页码数量*/ public static final int SHOW_PAGE_NUM = 10; /**记录集合*/ private List<T> pageList = null; /**当前页数*/ private int currPage = 1; /**每页显示大小*/ private int pageSize = 10; /**总页数*/ private int totalPage = 0; /**总记录数*/ private long totalCount = 0; /**开始页码*/ private int startPageNum; /**结束页码*/ private int endPageNum; public PageUtil(List<T> pageList, int currPage, long totalCount) { this(pageList, currPage, 0, totalCount); } public PageUtil(List<T> pageList, int currPage, int pageSize, long totalCount) { this.pageList = pageList; this.currPage = currPage; if (pageSize>0) this.pageSize = pageSize; this.totalCount = totalCount; initPageNumber(); } public List<T> getPageList() { return pageList; } public void setPageList(List<T> pageList) { this.pageList = pageList; } public int getCurrPage() { return currPage; } public void setCurrPage(int currPage) { this.currPage = currPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalPage() { this.totalPage = this.pageSize==0 ? 0 : (int)Math.ceil((double)this.totalCount / (double)this.pageSize); return this.totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public long getTotalCount() { return totalCount; } public void setTotalCount(long totalCount) { this.totalCount = totalCount; } public int getStartPageNum() { return startPageNum; } public void setStartPageNum(int startPageNum) { this.startPageNum = startPageNum; } public int getEndPageNum() { return endPageNum; } public void setEndPageNum(int endPageNum) { this.endPageNum = endPageNum; } public boolean hasPreviousPage() { return this.currPage > 1; } public boolean isHasPreviousPage() { return this.hasPreviousPage(); } public boolean hasNextPage() { return this.currPage < this.getTotalPage(); } public boolean isHasNextPage() { return this.hasNextPage(); } /** * 初始化页码(仿百度) * startPageNum 开始页码 * endPageNum 结束页码 * 通过开始页码和结束页码,可以循环得到分页页码列表 */ public void initPageNumber(){ if (this.getTotalPage() < SHOW_PAGE_NUM) { this.startPageNum = 1; this.endPageNum = this.getTotalPage(); } else { if (this.getCurrPage() <= SHOW_PAGE_NUM / 2 + 1) { this.startPageNum = 1; this.endPageNum = SHOW_PAGE_NUM; } else { this.startPageNum = this.getCurrPage() - (SHOW_PAGE_NUM / 2); this.endPageNum = this.getCurrPage() + (SHOW_PAGE_NUM / 2 - 1); if (this.endPageNum >= this.getTotalPage()) { this.endPageNum = this.getTotalPage(); this.startPageNum = this.getTotalPage() - SHOW_PAGE_NUM + 1; } } } } //转字符串(可选) @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); } }
以上类中方法 initPageNumber() 是参考了以下链接:
感谢分享者:http://blog.csdn.net/jasper_success/article/details/6664349
以下是测试类,模拟翻页测试,观看数据变化和页码情况。
文件:PageTest.java
package com.util; import java.util.ArrayList; import java.util.List; /** * 测试翻页 * Created by cxq on 2017/11/22. */ public class PageTest { public static void main(String[] arg0) { //测试页码翻页 for(int i=0; i<20; i++) { testPageNumber(i + 1, 10); } } public static void testPageNumber(int currPage, int pageSize){ //模拟查询数据 int totalCount = 195; List<String> list = getData((currPage - 1) * pageSize, pageSize, totalCount); PageUtil<String> page = new PageUtil<String>(list, currPage, totalCount); //输出数据 //System.out.println(page); //System.out.println("HasPreviousPage:" + page.isHasPreviousPage()); //System.out.println("HasPreviousPage:" + page.isHasNextPage()); //输出页码 System.out.print("当前第" + page.getCurrPage() + "页:"); for (int i=page.getStartPageNum(); i<=page.getEndPageNum(); i++) { if (i==page.getCurrPage()) { System.out.print(" " + i + " "); } else { System.out.print("[" + i + "]"); } } System.out.println(" "+page.getPageList()); } /** * 模拟查询数据 * @param offset 查询开启记录 * @param limit 查询多少条记录 * @param total 总记录 * @return 列表 */ public static List<String> getData(int offset, int limit, long total) { List<String> list = new ArrayList<String>(); int end = offset + limit; for (int i=offset; i<end; i++) { if (i>=total) break; list.add("item" + (i + 1)); } return list; } }
控制台输出:
当前第1页: 1 [2][3][4][5][6][7][8][9][10] [item1, item2, item3, item4, item5, item6, item7, item8, item9, item10] 当前第2页:[1] 2 [3][4][5][6][7][8][9][10] [item11, item12, item13, item14, item15, item16, item17, item18, item19, item20] 当前第3页:[1][2] 3 [4][5][6][7][8][9][10] [item21, item22, item23, item24, item25, item26, item27, item28, item29, item30] 当前第4页:[1][2][3] 4 [5][6][7][8][9][10] [item31, item32, item33, item34, item35, item36, item37, item38, item39, item40] 当前第5页:[1][2][3][4] 5 [6][7][8][9][10] [item41, item42, item43, item44, item45, item46, item47, item48, item49, item50] 当前第6页:[1][2][3][4][5] 6 [7][8][9][10] [item51, item52, item53, item54, item55, item56, item57, item58, item59, item60] 当前第7页:[2][3][4][5][6] 7 [8][9][10][11] [item61, item62, item63, item64, item65, item66, item67, item68, item69, item70] 当前第8页:[3][4][5][6][7] 8 [9][10][11][12] [item71, item72, item73, item74, item75, item76, item77, item78, item79, item80] 当前第9页:[4][5][6][7][8] 9 [10][11][12][13] [item81, item82, item83, item84, item85, item86, item87, item88, item89, item90] 当前第10页:[5][6][7][8][9] 10 [11][12][13][14] [item91, item92, item93, item94, item95, item96, item97, item98, item99, item100] 当前第11页:[6][7][8][9][10] 11 [12][13][14][15] [item101, item102, item103, item104, item105, item106, item107, item108, item109, item110] 当前第12页:[7][8][9][10][11] 12 [13][14][15][16] [item111, item112, item113, item114, item115, item116, item117, item118, item119, item120] 当前第13页:[8][9][10][11][12] 13 [14][15][16][17] [item121, item122, item123, item124, item125, item126, item127, item128, item129, item130] 当前第14页:[9][10][11][12][13] 14 [15][16][17][18] [item131, item132, item133, item134, item135, item136, item137, item138, item139, item140] 当前第15页:[10][11][12][13][14] 15 [16][17][18][19] [item141, item142, item143, item144, item145, item146, item147, item148, item149, item150] 当前第16页:[11][12][13][14][15] 16 [17][18][19][20] [item151, item152, item153, item154, item155, item156, item157, item158, item159, item160] 当前第17页:[11][12][13][14][15][16] 17 [18][19][20] [item161, item162, item163, item164, item165, item166, item167, item168, item169, item170] 当前第18页:[11][12][13][14][15][16][17] 18 [19][20] [item171, item172, item173, item174, item175, item176, item177, item178, item179, item180] 当前第19页:[11][12][13][14][15][16][17][18] 19 [20] [item181, item182, item183, item184, item185, item186, item187, item188, item189, item190] 当前第20页:[11][12][13][14][15][16][17][18][19] 20 [item191, item192, item193, item194, item195]
已下载:242 次
原创文章,转载请注明出处:https://www.weizhixi.com/article/3.html