Hibernate分頁

由於在Hibernate中直接提供了對數據庫定點定量的查詢方法,所以我採用的是第2種方法。

如:
從第1萬條開始取出100條記錄
Query q = session.createQuery("from Cat as c");

q.setFirstResult(10000);
q.setMaxResults(100);
List l = q.list();


具體實現

1.Pager類

package com.jpcf.db.helper;
import java.math.*;
public class Pager {
private int totalRows; //總行數
private int pageSize = 10; //每頁顯示的行數
private int currentPage; //當前頁號
private int totalPages; //總頁數
private int startRow; //當前頁在數據庫中的起始行
public Pager() {
}
public Pager(int _totalRows) {
totalRows = _totalRows;
totalPages=totalRows/pageSize;
int mod=totalRows%pageSize;
if(mod>0){
totalPages++;
}
currentPage = 1;
startRow = 0;
}
public int getStartRow() {
return startRow;
}
public int getTotalPages() {
return totalPages;
}
public int getCurrentPage() {
return currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalRows() {
return totalRows;
}
public void first() {
currentPage = 1;
startRow = 0;
}
public void previous() {
if (currentPage == 1) {
return;
}
currentPage--;
startRow = (currentPage - 1) * pageSize;
}
public void next() {
if (currentPage < totalPages) {
currentPage++;
}
startRow = (currentPage - 1) * pageSize;
}
public void last() {
currentPage = totalPages;
startRow = (currentPage - 1) * pageSize;
}
public void refresh(int _currentPage) {
currentPage = _currentPage;
if (currentPage > totalPages) {
last();
}
}
}

Pager類用於計算首頁、前一頁、下一頁、尾頁的在數據庫中的起始行,當前的頁碼。

2.PagerHelp類

package com.jpcf.db.helper;
import javax.servlet.http.*;
public class PagerHelper {
public static Pager getPager(HttpServletRequest httpServletRequest,
int totalRows) {
//定義pager對象,用於傳到頁面
Pager pager = new Pager(totalRows);
//從Request對象中獲取當前頁號
String currentPage = httpServletRequest.getParameter("currentPage");
//如果當前頁號爲空,表示爲首次查詢該頁
//如果不爲空,則刷新pager對象,輸入當前頁號等信息
if (currentPage != null) {
pager.refresh(Integer.parseInt(currentPage));
}
//獲取當前執行的方法,首頁,前一頁,後一頁,尾頁。
String pagerMethod = httpServletRequest.getParameter("pageMethod");
if (pagerMethod != null) {
if (pagerMethod.equals("first")) {
pager.first();
} else if (pagerMethod.equals("previous")) {
pager.previous();
} else if (pagerMethod.equals("next")) {
pager.next();
} else if (pagerMethod.equals("last")) {
pager.last();
}
}
return pager;
}
}

PageHelper這個類,我不用說應該也知道用來幹嘛了

3.DAO類

package com.jpcf.db.dao;
import com.jpcf.db.model.*;
import com.jpcf.db.helper.HibernateUtil;
import net.sf.hibernate.*;
import java.util.*;
import com.jpcf.db.controller.*;
public class VehiclePropertyDAO {
public Collection findWithPage(int pageSize, int startRow) throws
HibernateException {
Collection vehicleList = null;
Transaction tx = null;
try {
Session session = HibernateUtil.currentSession();
tx = session.beginTransaction();
Query q = session.createQuery("from VehicleProperty vp");
q.setFirstResult(startRow);
q.setMaxResults(pageSize);
vehicleList = q.list();
tx.commit();
} catch (HibernateException he) {
if (tx != null) {
tx.rollback();
}
throw he;
} finally {
HibernateUtil.closeSession();
}
return vehicleList;
}
public int getRows(String query) throws
HibernateException {
int totalRows = 0;
Transaction tx = null;
try {
Session session = HibernateUtil.currentSession();
tx = session.beginTransaction();
totalRows = ((Integer) session.iterate(query).next()).
intValue();
tx.commit();
} catch (HibernateException he) {
if (tx != null) {
tx.rollback();
}
throw he;
} finally {
HibernateUtil.closeSession();
}
return totalRows;
}
}
DAO類我就貼這些分頁需要的代碼了。
“from VehicleProperty vp”也可以用一個參數傳進來,有興趣的自己改一下吧

4.Action

下面是在Action中用到的代碼:/
public ActionForward queryWithPage(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletresponse) {
Collection clInfos = null;//用於輸出到頁面的記錄集合
int totalRows;//記錄總行數
VehiclePropertyDAO vehicleDAO = new VehiclePropertyDAO();
//取得當前表中的總行數
try {
totalRows = vehicleDAO.getRows("select count(*) from VehicleProperty");
} catch (Exception ex) {
servlet.log(ex.toString());
return actionMapping.findForward(Constants.FAILURE);
}
//通過PagerHelper類來獲取用於輸出到頁面的pager對象
Pager pager=PagerHelper.getPager(httpServletRequest,totalRows);
//取出從startRow開始的pageSize行記錄
try {
clInfos = vehicleDAO.findWithPage(pager.getPageSize(), pager.getStartRow());
} catch (Exception ex) {
servlet.log(ex.toString());
return actionMapping.findForward(Constants.FAILURE);
}
//把輸出的記錄集和pager對象保存到request對象中
httpServletRequest.setAttribute("CLINFOS", clInfos);
httpServletRequest.setAttribute("PAGER", pager);
return actionMapping.findForward(Constants.SUCCESS);
}

查詢語句select count(*) from VehicleProperty 也可以換成你需要的任意的條件(select count(*) from VehicleProperty where ..)


5.JSP頁面使用

下面就是在JSP中的應用了:

<td colspan="8" align="right" class="head">
第<bean:write name="PAGER" property="currentPage"/>頁
共<bean:write name="PAGER" property="totalPages"/>頁
<html:link action="/bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=first" paramName="PAGER" paramProperty="currentPage" paramId="currentPage">首頁</html:link>
<html:link action="/bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=previous" paramName="PAGER" paramProperty="currentPage" paramId="currentPage">上一頁</html:link>
<html:link action="/bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=next" paramName="PAGER" paramProperty="currentPage" paramId="currentPage">下一頁</html:link>
<html:link action="/bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=last" paramName="PAGER" paramProperty="currentPage" paramId="currentPage">尾頁</html:link>
</td>

解釋一下這一行:"/bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=first
method=queryWithPage 是由於我的Action繼承的是DispatchAction,需要一個method參數
pageMethod=first 是用來在PageHelper類中判斷執行哪個操作

總結

我做的這個也只是一個借鑑,還有很多沒有實現的,比如還可以加一下 go 直接到第n頁的功能。

其實最關鍵的是把當前頁號和要執行的是功能(上一頁,下一頁)的參數從頁面傳進來,在Action中就可以根據這兩個參數去取下一個頁面上要顯示的記錄集了
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章