分页封装

分页封装

1)

public class SimplePage implements Serializable {

private static final long serialVersionUID = 1L;
public static final int DEF_COUNT = 20;
protected int totalCount = 0;
protected int pageSize = 10;
protected int pageNo = 1;
protected int startIndex = 0;
public static int checkPageNo(Integer pageNo) {
return (pageNo == null || pageNo < 1) ? 1 : pageNo;
}

public SimplePage() {

}

public SimplePage(int pageNo, int pageSize, int totalCount) {
setTotalCount(totalCount);
setPageSize(pageSize);
setPageNo(pageNo);
adjustPageNo();
}

/**
* 调整页码,使其不超过最大页数
*/
public void adjustPageNo() {
if (pageNo == 1) {
return;
}
int totalPage = getTotalPage();
if (pageNo > totalPage) {
pageNo = totalPage;
}
startIndex = this.pageSize * (this.pageNo - 1);
}


public int getTotalCount() {
return totalCount;
}


public int getPageSize() {
return pageSize;
}


public int getPageNo() {
return pageNo;
}

public int getStartIndex() {
return startIndex;
}

public int getTotalPage() {
int totalPage = totalCount / pageSize;
if (totalPage == 0 || totalCount % pageSize != 0) {
totalPage++;
}
return totalPage;
}

/**
* 是否第一页
* @return
*/
public boolean isFirstPage() {
return pageNo <= 1;
}

/**
* 是否最后一页
* @return
*/
public boolean isLastPage() {
return pageNo >= getTotalPage();
}

/**
* 下一页页码
* @return
*/
public int getNextPage() {
if (isLastPage()) {
return pageNo;
} else {
return pageNo + 1;
}
}

public int getPretPage() {
if (isFirstPage()) {
return pageNo;
} else {
return pageNo - 1;
}
}

public void setTotalCount(int totalCount) {
if (totalCount < 0) {
this.totalCount = 0;
} else {
this.totalCount = totalCount;
}
}

public void setPageSize(Integer pageSize) {
if (pageSize == null || pageSize < 1) {
this.pageSize = DEF_COUNT;
} else {
this.pageSize = pageSize;
}
startIndex = this.pageSize * (this.pageNo - 1);
}

public void setPageNo(Integer pageNo) {
if (pageNo == null || pageNo < 1) {
this.pageNo = 1;
} else {
this.pageNo = pageNo;
}
startIndex = this.pageSize * (this.pageNo - 1);
}


}

2)

public class Pagination extends SimplePage implements Serializable {
private static final long serialVersionUID = 1L;
public final static String PAGE_CONDITION_CACHE = "PageCondition";
public final static String PAGE_CURRENT_NO_CACHE = "PageCurrentNo";
public final static String PAGE_SIZE_CACHE = "PageSize";
public final static String PAGE_TOTAL_COUNT_CACHE = "PageTotalCount";
public final static String PAGE_ALL_CONDITION_CACHE = "PageAllCondition";
private String id;
/**
* 检查页码是否超过最后一页
*/
private boolean checkPassLastPageNo = true;
/**
* 是否查询总条目数
*/
private boolean selectTotalCount = true;
private List<?> list;
private Object condition;
private Integer sort;
/**
* 初始条目偏移量
*/
private Integer offset = 0;
public Pagination() {
}
public Pagination(int pageNo, int pageSize, int totalCount) {
super(pageNo, pageSize, totalCount);
}
public Pagination(int pageNo, int pageSize, int totalCount, int offset) {
super(pageNo, pageSize, totalCount);
setOffset(offset);
}

public Pagination(int pageNo, int pageSize, int totalCount, List<?> list) {
super(pageNo, pageSize, totalCount);
this.list = list;
}


public int getFirstResult() {
return (pageNo - 1) * pageSize + offset;
}


public List<?> getList() {
return list;
}


public void setList(List<?> list) {
this.list = list;
}


public Object getCondition() {
return condition;
}


public void setCondition(Object condition) {
this.condition = condition;
}


public Integer getSort() {
return sort;
}


public void setSort(Integer sort) {
this.sort = sort;
}


public String getId() {
return id;
}


public void setId(String id) {
this.id = id;
}


public boolean isCheckPassLastPageNo() {
return checkPassLastPageNo;
}


public void setCheckPassLastPageNo(boolean checkPassLastPageNo) {
this.checkPassLastPageNo = checkPassLastPageNo;
}


public boolean isSelectTotalCount() {
return selectTotalCount;
}


public void setSelectTotalCount(boolean selectTotalCount) {
this.selectTotalCount = selectTotalCount;
}


public Integer getOffset() {
return offset;
}


public void setOffset(Integer offset) {
this.offset = offset;
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值