分頁其實很簡單,無非就是根據sql語句,加上限制條件,顯示從第幾條數據,到第幾條數據而已。
切入正題,先看一下下面的例子。
我有一張表sjdr_product_detail
首先查詢全部:
select * from sjdr_product_detail; //一共17條數據
分頁,就是通過sql語句的limit關鍵字來限制條件。
select * from sjdr_product_detail limit #{start},#{pageSize};
start 和 pageSize 都用 #{ } 標註起來,是想讓大傢知道,這兩個值是變量,即需要限制的條件。其實pageSize應該是可以固定的,比如每頁顯示3條,我們固定死。所以,下面主要的就是來得到這個start的值,那麼到底這個值怎麼得到呢?請看下面
實驗,找出規律
實驗,找出規律
首先,pageSize = 3 ,即每頁顯示條數為3條
首先,pageSize = 3 ,即每頁顯示條數為3條
查詢第1頁
#第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;
數據一目瞭然,那麼我們來研究一下,頁數、start、pageSize 之間的關系
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
規律:從以上不難看出: start = (當前頁-1)* pageSize
規律:從以上不難看出: start = (當前頁-1)* pageSize
0 = (1-1) * 3
3 = (2-1) * 3
6 = (3-1) * 3
9 = (4-1) * 3
12= (5-1) * 3
15= (6-1) * 3
既然我們得到瞭這個規律,那麼下面就容易多瞭。怎麼把它整合到我們的項目當中去?
首先,我們見一個分頁工具類PageUtil.class
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;
}
}
2 我這裡用的是mybatis
select count(1) from sjdr_product_detail;
select * from sjdr_product_detail order by P_ID DESC
limit #{skips},#{pageSize};
3 Controller
@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 detailList = this.detailService.queryByPage(page);
page.setRows(detailList);
//轉成json格式
WriteResponseUtil util = new WriteResponseUtil();
util.writeResponse(JsonUtil.toJSONString(page), res);
}
所以分頁查詢的時候,隻要傳遞頁數和每頁顯示條數即可。我這邊是倒敘查詢
查詢結果:
detail_page.htm?curr=1&size=3
{
"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 //總頁數
}
所以分頁查詢還是很簡單的。
希望給大傢帶來一點幫助。