主要思路:
1.定义分页接口(IPage),该接口定义了总记录数、总页数、每页记录数、当前页号、上一页、下一页、是否第一页、是否最后一页等方法
2.封装一个简单的分页工具类(SimplePage)实现了IPage接口,添加调整分页合理化方法adjustPage()
3.封装实用工具类Page类继承简单分页工具类SimplePage实现分布接口IPage,添加计算每页起始记录方法getFirstResult()
IPage接口代码如下:
/**
* 可分页接口
* @author xm
*
*/
public interface IPage {
/**
* 总记录数
* @return int
*/
public int getTotalCount();
/**
* 总页数
* @return int
*/
public int getTotalPage();
/**
* 每页记录数
* @return
*/
public int getPageSize();
/**
* 当前页号
* @return
*/
public int getPageNo();
/**
* 返回下页的页号
*
* @return int
*/
public int getNextPage();
/**
* 返回上页的页号
*
* @return int
*/
public int getPrePage();
/**
* 是否第一页
*
* @return boolean
*/
public boolean isFirstPage();
/**
* 是否最后一页
*
* @return boolean
*/
public boolean isLastPage();
}
SimplePage类代码:
/**
* 简单的分页控件
*
* @author xm
*/
public class SimplePage implements IPage {
private static final long serialVersionUID = 1L;
public static final int DEF_COUNT = 20;
protected int totalCount = 0;
protected int pageSize = 20;
protected int pageNo = 1;
/**
* 无参的构造方法
*/
public SimplePage() {
}
/**
* 有参的构造方法
*
* @param pageNo
* @param pageSize
* @param totalCount
*/
public SimplePage(int pageNo, int pageSize, int totalCount) {
if (totalCount <= 0) {
this.totalCount = 0;
} else {
this.totalCount = totalCount;
}
if (pageSize <= 0) {
this.pageSize = DEF_COUNT;
} else {
this.pageSize = pageSize;
}
if (pageNo <= 0) {
this.pageNo = 1;
} else {
this.pageNo = pageNo;
}
if (this.pageNo > getTotalPage()) {
this.pageNo = getTotalPage();
}
}
/**
* 调整分页参数,使合理化
*/
public void adjustPage() {
if (totalCount <= 0) {
totalCount = 0;
}
if (pageSize <= 0) {
pageSize = DEF_COUNT;
}
if (pageNo <= 0) {
pageNo = 1;
}
if (pageNo > getTotalPage()) {
pageNo = getTotalPage();
}
}
public int getPageNo() {
return pageNo;
}
public int getPageSize() {
return pageSize;
}
public int getTotalCount() {
return totalCount;
}
public int getTotalPage() {
int totalPage = totalCount / pageSize;
if (totalCount % pageSize != 0) {
totalPage++;
}
return totalPage;
}
public boolean isFirstPage() {
return pageNo <= 1;
}
public boolean isLastPage() {
return pageNo >= getTotalPage();
}
public int getNextPage() {
if (isLastPage()) {
return pageNo;
} else {
return pageNo + 1;
}
}
public int getPrePage() {
if (isFirstPage()) {
return pageNo;
} else {
return pageNo - 1;
}
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/**
* 如果大于总页数就等于总页数
*
* @param pageNo
*/
public void setPageNo(int pageNo) {
if (pageNo > getTotalPage()) {
this.pageNo = getTotalPage();
} else {
this.pageNo = pageNo;
}
}
}
Page类代码:(注意在创建类时要添加泛型T)
import java.util.List;
/**
* 可分页接口实体类
*
* @author xm
* @param <T>
*/
public class Page<T> extends SimplePage implements IPage {
/**
* 当前页面数据
*/
private List<T> list;
public Page() {
super();
}
public Page(int pageNo, int pageSize, int totalCount) {
super(pageNo, pageSize, totalCount);
}
/**
* 重载Page构造方法初始化List
*
* @param pageNo
* @param pageSize
* @param totalCount
* @param list
*/
public Page(int pageNo, int pageSize, int totalCount, List list) {
super(pageNo, pageSize, totalCount);
this.list = list;
}
/**
* 得到(当前页-1)*每页记录数
*
* @return int
*/
public int getFirstResult() {
return (pageNo - 1) * pageSize;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
本文介绍了一种分页接口的设计方法,包括定义分页接口IPage,实现简单分页工具类SimplePage,并通过Page类进一步封装以支持泛型列表。文章详细展示了各部分的实现代码。

547

被折叠的 条评论
为什么被折叠?



