以烂大街的商城项目为例介绍分页实体的成员定义:
首先需要创建一个list集合,可以使用ArrayList集合用来存储当前页中的商品信息。
分页的参数:
currentPageNum:当前页数,由用户指定。
pageSize:一页的商品数量,我们直接给值就行了
totalRecords:总记录数,从数据库中查询得到
计算出来的变量:
totalPageNum:总分页数,使用下面的三元运算符:
totalPageNum = (totalRecords%pageSize==0) ? (totalRecords/pageSize):(totalRecords/pageSize+1);
startIndex:每页开始记录的索引,
prePageNum:前一页,
nextPageNum:后一页,
totalPageNum:总页数,
按下面方法计算:
startIndex = (currentPageNum-1)*pageSize;
if(currentPageNum==1){
prePageNum = 1;//如果当前页是1,则前一页是一
}else{
prePageNum = currentPageNum-1; //否则前一页是当前页减1
}
if(currentPageNum==totalPageNum){
nextPageNum = totalPageNum;//如果当前页是最后一页,那么最后一页