动手封装一个完整的java分页工具类PageBean

原文链接: https://blog.csdn.net/guobinhui/article/details/80394707

转载自:https://blog.csdn.net/guobinhui/article/details/80394707

众所周知,web项目基本上都会用到动态分页功能,下面给大家整理写一个完整的PageBean工具类,大家把完整代码拷到自己项目里就可以直接用,本实例也适用于APP项目的翻页,废话不多说,直接上代码

import java.util.List;
 
/**
 * 作者:guobinhui 
* 创建时间:2018年5月21日
* 描述:动态分页Bean */ public class PageBean { private List pageData; private Integer currentPage = Integer.valueOf(1); private Integer pageSize = Integer.valueOf(10); private Integer totalCount; public int getPageCount(){ if (this.totalCount.intValue() % this.pageSize.intValue() == 0) { return this.totalCount.intValue() / this.pageSize.intValue(); } return this.totalCount.intValue() / this.pageSize.intValue() + 1; } public PageBean(List pageData, Integer totalCount) { this.pageData = pageData; this.totalCount = totalCount; } public PageBean() {} public boolean isFirst() { return (this.currentPage.intValue() == 1) || (this.totalCount.intValue() == 0); } public boolean isLast() { return (this.totalCount.intValue() == 0) || (this.currentPage.intValue() >= getPageCount()); } public boolean isHasNext() { return this.currentPage.intValue() < getPageCount(); } public boolean isHasPrev() { return this.currentPage.intValue() > 1; } public Integer getNextPage() { if (this.currentPage.intValue() >= getPageCount()) { return Integer.valueOf(getPageCount()); } return Integer.valueOf(this.currentPage.intValue() + 1); } public Integer getPrevPage() { if (this.currentPage.intValue() <= 1) { return Integer.valueOf(1); } return Integer.valueOf(this.currentPage.intValue() - 1); } public List getPageData() { return this.pageData; } public void setPageData(List pageData) { this.pageData = pageData; } public Integer getCurrentPage() { return this.currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; } public Integer getPageSize() { return this.pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public Integer getTotalCount() { return this.totalCount; } public void setTotalCount(Integer totalCount) { this.totalCount = totalCount; } }

项目service中,只需要根据参数page和pageSize这2个参数,就可以查出符合条件的记录(List),和总数totalCount,把这2个参数传入PageBean这个构造器中即可返回分页的所有数据,代码示例如下:

动手封装一个完整的java分页工具类PageBean_第1张图片

最终返回的数据结构如下:

{ "data":{
        "first": true,
        "hasNext": false,
        "hasPrev": false,
        "last": true,
        "nextPage": 1,
        "currentPage": 1,
        "pageCount": 1,
        "pageSize": 20,
        "prevPage": 1,
        "totalCount ": 3,
        "pageData":[{
             "voipId ":"2142343254353456436",
             "voipStateDate ":"2018-04-23 14:02:00",
             "objName":"guoxiansheng",
             "voipStatus":1 
        }]
   },
   "errorCode":10000,
   "erorInfo":"创建成功"
}

 

 

你可能感兴趣的:(Java相关)