分页其实很简单,无非就是根据sql语句,加上限制条件,显示从第几条数据,到第几条数据而已。
切入正题,先看一下下面的例子。
我有一张表sjdr_product_detail
首先查询全部:
<code class="hljs cs">select * from sjdr_product_detail; //一共17条数据</code>
<img alt="这里写图片描述" src="http://img.bitscn.com/upimg/allimg/c150817/143aO91N5940-102J7.jpg" title="\" />
分页,就是通过sql语句的limit关键字来限制条件。
<code class="hljs cs"><code class="hljs haml">select * from sjdr_product_detail limit #{start},#{pageSize}; </code></code>
<code class="hljs haml">start 和 pageSize 都用 #{ } 标注起来,是想让大家知道,这两个值是变量,即需要限制的条件。其实pageSize应该是可以固定的,比如每页显示3条,我们固定死。所以,下面主要的就是来得到这个start的值,那么到底这个值怎么得到呢?请看下面
<code class="hljs haml"><b>实验,找出规律</b>
<code class="hljs haml"><b>首先,pageSize = 3 ,即每页显示条数为3条</b>
<code class="hljs haml">查询第1页
<code class="hljs cs"><code class="hljs haml"><code class="hljs cs">#第1页 select * from sjdr_product_detail limit 0,3; #第2页 select * from sjdr_product_detail limit 3,3; #第3页 select * from sjdr_product_detail limit 6,3; #第4页 select * from sjdr_product_detail limit 9,3; #第5页 select * from sjdr_product_detail limit 12,3; #第6页 select * from sjdr_product_detail limit 15,3;</code></code></code>
<code class="hljs haml"><code class="hljs cs"><img alt="这里写图片描述" src="http://img.bitscn.com/upimg/allimg/c150817/143aO91R1X0-119328.jpg" title="\" />
<code class="hljs haml"><code class="hljs cs">数据一目了然,那么我们来研究一下,页数、start、pageSize 之间的关系
<code class="hljs haml"><code class="hljs cs">当前页 |
<code class="hljs haml"><code class="hljs cs">start |
<code class="hljs haml"><code class="hljs cs">pageSize |
<code class="hljs haml"><code class="hljs cs">1 |
<code class="hljs haml"><code class="hljs cs">0 |
<code class="hljs haml"><code class="hljs cs">3 |
<code class="hljs haml"><code class="hljs cs">2 |
<code class="hljs haml"><code class="hljs cs">3 |
<code class="hljs haml"><code class="hljs cs">3 |
<code class="hljs haml"><code class="hljs cs">3 |
<code class="hljs haml"><code class="hljs cs">6 |
<code class="hljs haml"><code class="hljs cs">3 |
<code class="hljs haml"><code class="hljs cs">4 |
<code class="hljs haml"><code class="hljs cs">9 |
<code class="hljs haml"><code class="hljs cs">3 |
<code class="hljs haml"><code class="hljs cs">5 |
<code class="hljs haml"><code class="hljs cs">12 |
<code class="hljs haml"><code class="hljs cs">3 |
<code class="hljs haml"><code class="hljs cs">6 |
<code class="hljs haml"><code class="hljs cs">15 |
<code class="hljs haml"><code class="hljs cs">3 |
<code class="hljs haml"><code class="hljs cs"><b>规律:从以上不难看出: start = (当前页-1)* pageSize</b>
<code class="hljs cs"><code class="hljs haml"><code class="hljs cs">0 = (1-1) * 3 3 = (2-1) * 3 6 = (3-1) * 3 9 = (4-1) * 3 12= (5-1) * 3 15= (6-1) * 3 </code></code></code>
<code class="hljs haml"><code class="hljs cs">既然我们得到了这个规律,那么下面就容易多了。怎么把它整合到我们的项目当中去?
<code class="hljs haml"><code class="hljs cs">首先,我们见一个分页工具类PageUtil.class
<code class="hljs cs"><code class="hljs haml"><code class="hljs cs"><code class="hljs cs">package cn.sg.util; /** * @author ZSL */ import java.util.ArrayList; import java.util.List; public class PageUtil { public PageUtil(int curr,int size,int total){ this.pageSize = size; this.totalPage = total % size == 0 ? total/size : (total/size) + 1; this.currPage = curr < 1 ? 1 : curr; this.currPage = curr > this.totalPage ? this.totalPage : this.currPage; skips = (this.currPage - 1) * this.pageSize; this.totalCount = total; } private int currPage = 1; //当前页 private int pageSize = 10; //每页显示条数 private int totalPage = 0; //总页数 private int totalCount = 0; //总条数 private int skips = 0; //跳过的值,即sql中的start值 private List rows = new ArrayList(); //用来存放查询到的数据集合 ////////////////////////////////////////////////////// //getter、setter 方法 public int getCurrPage() { return currPage; } public void setCurrPage(int currPage) { this.currPage = currPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public List getRows() { return rows; } public void setRows(List rows) { this.rows = rows; } public int getSkips() { return skips; } public void setSkips(int skips) { this.skips = skips; } } </code></code></code></code>
<code class="hljs haml"><code class="hljs cs"><code class="hljs cs">2 我这里用的是mybatis
<code class="hljs cs"><code class="hljs haml"><code class="hljs cs"><code class="hljs cs"><code class="hljs xml"><!-- 查询数量 --> <select id="getCount" resulttype="java.lang.Integer"> select count(1) from sjdr_product_detail; </select> <!-- 分页查询 --> <select id="queryByPage" resultmap="detail_resultMap" resulttype="Detail"> select * from sjdr_product_detail order by P_ID DESC limit #{skips},#{pageSize}; </select></code></code></code></code></code>
<code class="hljs haml"><code class="hljs cs"><code class="hljs cs"><code class="hljs xml">3 Controller
<code class="hljs cs"><code class="hljs haml"><code class="hljs cs"><code class="hljs cs"><code class="hljs xml"><code class="hljs java">@RequestMapping("detail_page.htm") public void queryByPage(HttpServletRequest req, Model model){ //当前页 int curr = Integer.valueOf(req.getParameter("curr")); //每页显示条数 int size = Integer.parseInt(req.getParameter("size")); //查询数量 int total = this.detailService.getCount(); PageUtil page = new PageUtil(curr, size, total); List<detail> detailList = this.detailService.queryByPage(page); page.setRows(detailList); //转成json格式 WriteResponseUtil util = new WriteResponseUtil(); util.writeResponse(JsonUtil.toJSONString(page), res); } </detail></code></code></code></code></code></code>
<code class="hljs haml"><code class="hljs cs"><code class="hljs cs"><code class="hljs xml"><code class="hljs java">所以分页查询的时候,只要传递页数和每页显示条数即可。我这边是倒叙查询<br />
查询结果:
<code class="hljs haml"><code class="hljs cs"><code class="hljs cs"><code class="hljs xml"><code class="hljs java">detail_page.htm?curr=1&size=3
<code class="hljs cs"><code class="hljs haml"><code class="hljs cs"><code class="hljs cs"><code class="hljs xml"><code class="hljs java"><code class="hljs cs">{ "currPage": 1, //第一页 "pageSize": 3, //每页显示条数 "rows": [ //查询数据集合 { "p_ID": "17", "p_PIC": "17777" }, { "p_ID": "16", "p_PIC": "16666" }, { "p_ID": "15", "p_PIC": "15555" } ], "skips": 0, //相当于sql中的start "totalCount": 17, //总条数 "totalPage": 6 //总页数 }</code></code></code></code></code></code></code>
<code class="hljs haml"><code class="hljs cs"><code class="hljs cs"><code class="hljs xml"><code class="hljs java"><code class="hljs cs">所以分页查询还是很简单的。<br>
希望给大家带来一点帮助。