博客管理系统项目中使用的是PageHelper的分页插件,这里回忆一下手写实现分页的方法:
1.分页Bean
public class PageBean<T> {
private int pc;// 当前页码page code
//总页数tp:由tr/ps计算可得
private int tr;// 总记录数total record
private int ps;// 每页记录数page size
private List<T> beanList;// 当前页的记录
……
}
2.分页在各层中的处理

(1)DAO
public PageBean<Customer> findAll(int pc, int ps) throws SQLException {
String sql = "select count(*) from t_customer";
int tr = ((Number)qr.query(sql, new ScalarHandler())).intValue();
sql = "select * from t_customer limit ?,?";//使用limit来限制查询记录数
List<Customer> customerList = qr.query(sql,
new BeanListHandler<Customer>(Customer.class), (pc-1)*ps, ps);
//将查询的结果放到特定的bean中
PageBean<Customer> pb = new PageBean<Customer>();
pb.setPc(pc);
pb.setPs(ps);
pb.setTr(tr);
pb.setDatas(customerList);
//返回bean
return pb;
}
(2)serlvet(controller)

这篇博客回顾了如何在没有使用PageHelper分页插件的情况下手动实现分页查询。内容包括创建分页Bean、在DAO和Servlet(Controller)层处理分页逻辑,以及计算分页页码列表的详细公式,确保在页面上正确显示分页链接。
最低0.47元/天 解锁文章
678

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



