分页公式

分页
1 什么分页
第N页/共M页 首页 上一页 1 2 3 4 5 6 7 8 9 10 下一页 尾页 口go

  分页的优点:只查询一页,不用查询所有页!

2 分页数据
页面的数据都是由Servlet传递来的!
Servlet:
 当前面:pageCode,pc;
 pc:如果页面没有传递当前页码,那么Servlet默认是第一页,或者按页面传递的来准!
 总页数:totalPages,tp
 tp:总记录数/每页记录数
 总记录数:totalRecored,tr
 tr:dao来获取,select count(*) from t_customer;
 每页记录数:业务数据或叫系统数据!10行!
 当前页数据:beanList
 url

3 分页Bean的设计
这些分页数据总要在各层之间来回的传递!
我们把这些分页数据封装到一个javabean中,它就叫分页Bean,例如:PageBean
PageBean.java
public class PageBean<T> {
private int pc;// 当前页码page code
// private int tp;// 总页数total page
private int tr;// 总记录数total record
private int ps;// 每页记录数page size
private List<T> beanList;// 当前页的记录
……
}

4 分页在各层中的处理
 页面:给出分页相关的链接们!
 页面需要给Servlet传递什么:有可能传递pc
 Servlet:创建PageBean对象,给PageBean所有的属性赋值,然后传递给页面;
 Servlet需要给DAO传递pc、ps
 Service:略
 Dao:
 tr:select count(*) t_customer
 beanList:select * from t_customer limit x, y

5 显示分页页码列表
1 2 3 4 5 6 7 8 9 10
 最多显示多少个页码!定为10;
 当前页,在页码列表中的位置,定为6;

只需要当前页码来定出来页码列表!
定下来页码列表只需要两样数据:
 begin
 end
10 11 12 13 14 (15) 16 17 18 19
需要使用pc来推算出begin和end
begin = pc – 5
end = pc + 4
计算公式:
 如果总页数<=10(列表长度),那么begin=1,end=总页数
 使用公式计算;begin=pc-5, end=pc + 4;
 头溢出:当begin<1时,让begin=1
 尾溢出:当end>${tp}时,让end=${tp}

6. 在超链接中要保留参数
当使用多条件查询后,然后在点击第2 页时,这个第2页超链接没有条件了,所以会丢失条件,所以我们需要在页面上的所有链接都要保留条件!
我们要把条件以一个字符串的形式保存到PageBean的url中!这个任务交给Servlet!

原文地址:https://www.cnblogs.com/luowen/p/4320839.html