一个常用的Java分页对象
闲空之时,给大家分享一个常用的Java分页对象,希望对大家有所帮助。代码如下:
public class Page {
private int pageNo = 1;//第几页,页码,默认为第一页
private int pageSize = 15;//页面大小
private long totalRecord;//总计里数目
private long totalPage;//总页数
public Page() {
}
public Page(int pageNo){
setPageNo(pageNo);
}
public Page(int pageNo,int pageSize){
setPageNo(pageNo);
setPageSize(pageSize);
}
public Page(int pageNo,int pageSize,long totalRecord){
setPageNo(pageNo);
setPageSize(pageSize);
setTotalRecord(totalRecord);
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
if(pageNo>0){
this.pageNo = pageNo;
}
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
if(pageSize>0){
this.pageSize = pageSize;
}
}
public long getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(long totalRecord) {
this.totalRecord = totalRecord;
long totalPage = totalRecord % pageSize == 0 ? totalRecord/pageSize:totalRecord/pageSize+1;
this.setTotalPage(totalPage);
}
public long getTotalPage() {
return totalPage;
}
public void setTotalPage(long totalPage) {
this.totalPage = totalPage;
}
//有的时候会用到
public int getOffset(){return (pageNo-1)*pageSize;}//每一页第一条数目的索引
public int getEndPage(){return pageNo * pageSize-1;}//每一页最后一条数目的索引
}
写代码的时候,要有清晰的思路再下手。