Maison > Article > base de données > 数据库连接池
计算确定数据表一共分几页:101/pageSize=100/10=10页。rows = select count(1) from users;方法1:pageCount = rows/pageSize + (rows%pageSize==0?0:1); 方法2:pageCount = (rows+ (pageSize-1))/(pageSize);public void doGet(HttpServletRequest req,
<span>计算确定数据表一共分几页: 101/pageSize=100/10=10页。 rows = select count(1) from users; 方法1:pageCount = rows/pageSize + (rows%pageSize==0?0:1); 方法2:pageCount = (rows+ (pageSize-1))/(pageSize); public void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException { //第一步:定义每页显示多少行 int pageSize = 10; try{ //第二步:获取数据表中有多少行 QueryRunner run = new QueryRunner(DataSourceUtils.getDatasSource()); String sql = "select count(*) from users"; Object o = run.query(sql,new ScalarHandler()); int rows = Integer.parseInt(o.toString()); //第三步:计算一共分多少页 int pageCount= rows/pageSize+(rows%pageSize==0?0:1); //将页数放到req req.setAttribute("pageCount",pageCount); }catch(Exception e){ e.printStackTrace(); } //转发到 req.getRequestDispatcher("/jsps/show.jsp").forward(req, response); } </span>
用户请求的面码 |
start |
算法: |
1 |
Limit 0,pageSize |
Start = (currentPage-1)*pageSize |
2 |
Limit 10,pageSize |
|
3 |
Limit 20,pageSize |
每个页面,最多显示10个页码
pageNum=10;
startNo
endNo
当前页码 |
页码范围 |
算法 |
1 |
1~10 |
If(currentPage 1~10 |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
2~11 |
6-4=2 = 6-(pageNum/2-1)=2 = startNo endNo = startNo+(pageNum-1)=11 |
7 |
3~12 |
|
8 |
4~13 |
|
9 |
|
|
10 |
|
|
11 |
|
11-4 = 7 endNo = 7+9=16 endNo = 11; startNo = 2 = endNo-(pageNum-1);
|
|
|