核心sql
i是第幾頁,itemNum是每頁顯示的數據條數
select * from (
select e.*,rownum rn from (
select * from employee where 1=1 order by empno) e where rownum <(i*itemNum))
where rn >(i-1)*itemNum
大體思路
使用Page類封裝(Page當做Dao類中某個方法的參數,Dao類中需要有個方法來獲得獲得滿足條件的記錄總數
每次點擊查詢,重新獲得總記錄數
首次進入某個需要顯示分頁的jsp頁面,先創建一個Page對象,設置頁號為第一頁,並同時使用當前的這個Page,從Dao類中查詢,得到第一頁的List數據,使用session.setAttribute()
方法,把Page對象和List數據存入到session中
第二次則是將pageNo的請求參數獲得,把session中的Page對象取出,設置pageNo,之后再使用這個Page,從Dao類中獲得新的一頁的List數據,把Page對象和List數據再次存入session中
之后,為分頁按鈕設置好href鏈接(上一頁,下一頁,最后一頁,a標簽定義herf,帶上頁號的參數 query.jsp?pageNo=?
//使用EL表達式讀取Page對象中的數據
<a href="query.jsp?pageNo=${page.pageNo}"></a>
<tr >
<td colspan="2"></td>
<td colspan="5">
<c:if test="${not employeePage.first}">
<a href="doQuery.jsp?pageNo=1">首頁</a>
</c:if>
<c:if test="${employeePage.previous}">
<a href="doQuery.jsp?pageNo=${employeePage.prevNo}">上一頁</a>
</c:if>
<%--如果有下一頁,就顯示下一頁的鏈接 --%>
<c:if test="${employeePage.next}">
<a href="doQuery.jsp?pageNo=${employeePage.nextNo}">下一頁</a>
</c:if>
<c:if test="${not employeePage.last}">
<a href="doQuery.jsp?pageNo=${employeePage.totalPage}">尾頁</a>
</c:if>
</td>
<td>
<span>當前第${employeePage.pageNo}頁/總共${employeePage.totalPage}頁</span>
</td>
</tr>
Page類代碼
/**
* 分頁控制類,封裝了相關分頁時所需的信息,包括:<br>
* <pre>
* pageNo - 頁號
* pageSize - 每頁顯示記錄數
* totalRow - 總行數
* totalPage - 總頁數
* previous - 是否有上一頁
* next - 是否有下一頁
* first - 是否是每一頁
* last - 是否是最后一頁
* firstIndex -當前頁號的開頭索引 如頁號為2,每頁顯示記錄數為5,當前的頁號的開頭索引為6
* lastIndex -當前頁號的末尾索引 如頁號為2,每頁顯示記錄數為5,當前的頁號的末尾索引為10
* </pre>
*
* @param <T> 查詢條件對象。Map 或者 POJO
* @author starsone
* <p>
* pageSize=10;
* 1頁: 第1~10條記錄
* 2頁: 第11~20第記錄
* .....
* 【第一頁】【上一頁】【下一頁】【最后一頁】
* Page<Book> page = new Page<Book>();
*/
public class Page<T> {
//預定常量:每頁的行數
public static final int R5 = 5;
public static final int R10 = 10;
public static final int R20 = 20;
public static final int R50 = 50;
public static final int R100 = 100;
public static final int DEFAULT_SIZE = R10;
//總行數
private int totalRow = 0;
//當前頁號
private int pageNo = 1;
//每頁的記錄數
public int pageSize = DEFAULT_SIZE;
//總頁數
private int totalPage = 0;
//是否有上一頁 <c:if test=“${sessionScope.page.previous}”> </c:if>
private boolean previous;
//是否有下一頁
private boolean next;
//是否是第一頁
private boolean first;
//是否是最后一頁
private boolean last;
//當前頁數據首條記錄索引 每頁10, 當前2頁:11~20
private int firstIndex;
//當前頁數據最后條記錄索引
private int lastIndex;
//查詢條件對象
private T queryObject;
public Page() {
this(0);
}
/**
* @param totalRow 總記錄數
*/
public Page(int totalRow) { // 101
this(totalRow, DEFAULT_SIZE);
}
/**
* @param totalRow 總記錄數
* @param pageSize 每頁記錄數
*/
public Page(int totalRow, int pageSize) {
this.totalRow = totalRow;
this.pageSize = pageSize;
//根據記錄數自動算出總頁數
if (totalRow % pageSize == 0) {
this.totalPage = totalRow / pageSize;
} else {
this.totalPage = totalRow / pageSize + 1;
}
}
/**
* @param queryObject 查詢條件
*/
public Page(T queryObject, int pageSize, int totalRow) {
this.queryObject = queryObject;
this.totalRow = totalRow;
this.pageSize = pageSize;
//根據記錄數自動算出總頁數
if (totalRow % pageSize == 0) {
this.totalPage = totalRow / pageSize;
} else {
this.totalPage = totalRow / pageSize + 1;
}
}
/**
* @param totalRow 總記錄數
* @param queryObject 查詢條件
*/
public Page(int totalRow, T queryObject) {
setTotalRow(totalRow);
this.queryObject = queryObject;
}
/**
* 得到總記錄數
*
* @return
*/
public int getTotalRow() {
return totalRow;
}
/**
* @param totalRow
*/
public void setTotalRow(int totalRow) {
this.totalRow = totalRow;
}
/**
* 得到當前的頁號
*
* @return
*/
public int getPageNo() {
return pageNo;
}
/**
* 得到下一頁的頁號
*
* @return
*/
public int getNextNo() {
if (this.getNext()) {
return pageNo + 1;
} else {
return totalPage;
}
}
/**
* 得到上一頁的頁號
*
* @return
*/
public int getPrevNo() {
if (this.getPrevious()) {
return pageNo - 1;
} else {
return pageNo;
}
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
/**
* 得到每頁顯示的記錄數
*
* @return
*/
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
/**
* 得到總頁數
*
* @return
*/
public int getTotalPage() {
if (totalRow % pageSize == 0) {
this.totalPage = totalRow / pageSize;
} else {
this.totalPage = totalRow / pageSize + 1;
}
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
/**
* 是否有下一頁
*
* @return
*/
public boolean getNext() {
if (pageNo == 1 && pageNo < getTotalPage()) {
return true;
}
if (pageNo > 1 && pageNo < getTotalPage()) {
return true;
} else {
return false;
}
}
/**
* 是否有上一頁
*
* @return
*/
public boolean getPrevious() {
if (getPageNo() > 1) {
return true;
} else {
return false;
}
}
/**
* 是否是第一頁
*
* @return
*/
public boolean getFirst() {
return (getPageNo() > 1 ? false : true);
}
/**
* 是否是最后一頁
*
* @return
*/
public boolean getLast() {
return (getPageNo() == getTotalPage() ? true : false);
}
/**
* 得到本頁數據首條記錄索引
*
* @return
*/
public int getFirstIndex() {
int i = getPageNo(); //pageNo = 3
return ((i < 1 ? 1 : i) - 1) * getPageSize() + 1;
}
/**
* 得到本頁數據最后一條記錄索引
*
* @return
*/
public int getLastIndex() {
return getFirstIndex() + this.getPageSize() - 1;
}
/**
* 得到查詢分頁的條件
*
* @return
*/
public T getQueryObject() {
return queryObject;
}
/**
* 設置查詢分頁的條件
*
* @return
*/
public void setQueryObject(T queryObject) {
this.queryObject = queryObject;
//this.totalRow = new EmployeeDao().countForQuery(((Employee) queryObject));
//根據記錄數自動算出總頁數
if (totalRow % pageSize == 0) {
this.totalPage = totalRow / pageSize;
} else {
this.totalPage = totalRow / pageSize + 1;
}
}
// 便於調試
@Override
public String toString() {
return "Page [是否是第一頁:" + getFirst()
+ ", 是否是最后頁:" + getLast()
+ ", 是否有上一頁:" + getPrevious()
+ ", 是否有下一頁:" + getNext()
+ ", 當前的頁號:" + getPageNo()
+ ", 每頁記錄數:" + getPageSize()
+ ",開始索引:" + getFirstIndex()
+ ",末尾索引:" + getLastIndex()
+ ", 總頁數=" + getTotalPage()
+ ", 總記錄數=" + getTotalRow() + "]";
}
}
使用范例
/* public static void main(String[] args) {
// 模擬頁頁提交的查詢條件
Employee emp = new Employee();
Page<Employee> page = new Page<Employee>(13);
// 設置(保存)查詢條件
page.setQueryObject(emp);
//設置 默認每頁顯示的記錄數
page.setPageSize(Page.R5);
// 設置當前頁號
page.setPageNo(3);
System.out.println(page);
// session.setAttribute("empPage", page);
}*/
select * from (select e.*,rownum rn from (select * from emploee where order by empno) where rownum <= (i*itemNUm) ) where rn >(i-1)*itemNum