這篇文章主要講解了“Struts+Hibernate分頁(yè)怎么實(shí)現(xiàn)”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“Struts+Hibernate分頁(yè)怎么實(shí)現(xiàn)”吧!
創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比澄江網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式澄江網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋澄江地區(qū)。費(fèi)用合理售后完善,十余年實(shí)體公司更值得信賴。
一、在Struts中分頁(yè)有兩種結(jié)構(gòu):
1. 在Action中通過(guò)DAO查詢出所有的記錄,然后加到session或request對(duì)象中,傳到客戶端,由JSP進(jìn)行分頁(yè)。這種方法對(duì)于在數(shù)據(jù)量少的時(shí)候很方便,也不影響速度。
2.在Action中每次通過(guò)DAO只查詢出一頁(yè)的記錄,再傳給JSP頁(yè)面。這種結(jié)構(gòu)對(duì)于數(shù)據(jù)量大的程序很好,但對(duì)于數(shù)據(jù)量小的情況,會(huì)增加對(duì)服務(wù)器的請(qǐng)求,加大服務(wù)器的負(fù)載。
二、Hibernate查詢
由于在Hibernate中直接提供了對(duì)數(shù)據(jù)庫(kù)定點(diǎn)定量的查詢方法,所以我采用的是第2種方法。
如:從第1萬(wàn)條開(kāi)始取出100條記錄
Query q = session.createQuery("from Cat as c"); q.setFirstResult(10000); q.setMaxResults(100); List l = q.list(); |
三、具體實(shí)現(xiàn)
1.Pager類
package com.jpcf.db.helper;
import java.math.*;
public class Pager { private int totalRows; //總行數(shù) private int pageSize = 10; //每頁(yè)顯示的行數(shù) private int currentPage; //當(dāng)前頁(yè)號(hào) private int totalPages; //總頁(yè)數(shù) private int startRow; //當(dāng)前頁(yè)在數(shù)據(jù)庫(kù)中的起始行
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類用于計(jì)算首頁(yè)、前一頁(yè)、下一頁(yè)、尾頁(yè)的在數(shù)據(jù)庫(kù)中的起始行,當(dāng)前的頁(yè)碼。
2.PagerHelp類
package com.jpcf.db.helper; import javax.servlet.http.*; public class PagerHelper { public static Pager getPager(HttpServletRequest httpServletRequest, int totalRows) { |
//定義pager對(duì)象,用于傳到頁(yè)面
Pager pager = new Pager(totalRows); |
//從Request對(duì)象中獲取當(dāng)前頁(yè)號(hào)
String currentPage = httpServletRequest.getParameter("currentPage"); |
//如果當(dāng)前頁(yè)號(hào)為空,表示為***查詢?cè)擁?yè)
//如果不為空,則刷新pager對(duì)象,輸入當(dāng)前頁(yè)號(hào)等信息
if (currentPage != null) { pager.refresh(Integer.parseInt(currentPage)); } |
//獲取當(dāng)前執(zhí)行的方法,首頁(yè),前一頁(yè),后一頁(yè),尾頁(yè)。
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這個(gè)類,我不用說(shuō)應(yīng)該也知道用來(lái)干嘛了
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類我就貼這些分頁(yè)需要的代碼了。
“from VehicleProperty vp”也可以用一個(gè)參數(shù)傳進(jìn)來(lái),有興趣的自己改一下吧
4.Action
下面是在Action中用到的代碼:
public ActionForward queryWithPage(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletresponse) { |
Collection clInfos = null;//用于輸出到頁(yè)面的記錄集合
int totalRows;//記錄總行數(shù)
VehiclePropertyDAO vehicleDAO = new VehiclePropertyDAO(); |
//取得當(dāng)前表中的總行數(shù)
try { totalRows = vehicleDAO.getRows("select count(*) from VehicleProperty"); } catch (Exception ex) { servlet.log(ex.toString()); return actionMapping.findForward(Constants.FAILURE); } |
//通過(guò)PagerHelper類來(lái)獲取用于輸出到頁(yè)面的pager對(duì)象
Pager pager=PagerHelper.getPager(httpServletRequest,totalRows); |
//取出從startRow開(kāi)始的pageSize行記錄
try { clInfos = vehicleDAO.findWithPage(pager.getPageSize(), pager.getStartRow()); } catch (Exception ex) { servlet.log(ex.toString()); return actionMapping.findForward(Constants.FAILURE); } |
//把輸出的記錄集和pager對(duì)象保存到request對(duì)象中
httpServletRequest.setAttribute("CLINFOS", clInfos); httpServletRequest.setAttribute("PAGER", pager); return actionMapping.findForward(Constants.SUCCESS); |
查詢語(yǔ)句select count(*) from VehicleProperty 也可以換成你需要的任意的條件(select count(*) from VehicleProperty where ..)
5.JSP頁(yè)面使用
下面就是在JSP中的應(yīng)用了:
第頁(yè) 共頁(yè) ="/bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=first 上一頁(yè) 下一頁(yè) 尾頁(yè) " paramName="PAGER" paramProperty="currentPage" paramId="currentPage">首頁(yè) |
解釋一下這一行:"/bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=first
method=queryWithPage 是由于我的Action繼承的是DispatchAction,需要一個(gè)method參數(shù)
pageMethod=first 是用來(lái)在PageHelper類中判斷執(zhí)行哪個(gè)操作
感謝各位的閱讀,以上就是“Struts+Hibernate分頁(yè)怎么實(shí)現(xiàn)”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)Struts+Hibernate分頁(yè)怎么實(shí)現(xiàn)這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!