解析Hibernate在JSP下的分頁技術(shù)
分頁在任何系統(tǒng)中都是非常頭疼的事情,有的數(shù)據(jù)庫在語法上支持分頁,而有的數(shù)據(jù)庫則需要使用可滾動(dòng)游標(biāo)來實(shí)現(xiàn),并且在不支持可滾動(dòng)游標(biāo)的系統(tǒng)上只能使用單向游標(biāo)逐步接近要取得的數(shù)據(jù)。Hibernate提供了一個(gè)支持跨系統(tǒng)的分頁機(jī)制,這樣無論底層是什么樣的數(shù)據(jù)庫都能用統(tǒng)一的接口進(jìn)行分頁操作。本文講述的是JSP下的Hibernate分頁技術(shù)。
這是我知道的代碼最少且最簡(jiǎn)潔的一種Hibernate分頁技術(shù)了,自己懶,所以拼命減少代碼量,呵呵。下面用人能看得懂的語言細(xì)說一下,關(guān)于Hibernate的分頁技術(shù),無外乎兩種:
1. 從數(shù)據(jù)庫中取得記錄,在內(nèi)存中再劃分。但如果遇到記錄數(shù)很大的時(shí)候效率很成問題。
2. 采用Hibernate的物理分頁,每次只是取一頁。從客戶端傳進(jìn)來的是第幾頁和每頁多少條記錄,要首先查詢符合記錄的總記錄數(shù),再根據(jù)總記錄數(shù)和當(dāng)前頁,每頁記錄數(shù)可以算出要取的是數(shù)據(jù)庫中的第幾條記錄。但2次查詢不可避免了。
所以總結(jié)了兩種方式的優(yōu)劣,如果數(shù)據(jù)量不是非常大的話(百萬以上),采用***種方法,否則可選擇第二種。由于我要操作的數(shù)據(jù)庫信息量沒有達(dá)到大的標(biāo)準(zhǔn),所以我采用了***種方法,下面細(xì)說。
首先看一下我的一個(gè)action:
- public ActionForward queryZcDoc(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- {
- IZcDocService zcDocService=(IZcDocService)
- Application.getInstance().getBean("zcDocServiceProxy");
- List docList=zcDocService.queryZcDoc();
- request.setAttribute("doc", subMessList);
- return mapping.findForward("queryDoc");
- }
很簡(jiǎn)單的代碼,就是查詢數(shù)據(jù),扔到一個(gè)List里面,然后setAttribute,再在jsp頁面顯示就可以了。
接下來談分頁,考慮到了簡(jiǎn)潔性和通用性,我把分頁的代碼單獨(dú)封裝到了一個(gè)類里面去,下面看看這個(gè)類:
- public class Fenye {
- public List fenye(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response){
- List list=(ArrayList) request.getAttribute("list");
- /*
這里有人可能就看不懂了,為什么要帶這些參數(shù)?因?yàn)槲疑厦娴腶ction方法是分頁之前的方法,所以不能看出來。
下面貼一下用到分頁之后的action方法:
- public ActionForward queryZcDoc(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- IZcDocService zcDocService=(IZcDocService)Application.getInstance().
- getBean("zcDocServiceProxy");
- List docList=zcDocService.queryZcDoc();
- request.setAttribute("list", docList);
- List subMessList=new Fenye().fenye(mapping, form, request, response);
- request.setAttribute("doc", subMessList);
- return mapping.findForward("queryDoc");
- }
和上面的一比較,其實(shí)就多了兩行代碼,為的就是保持頁面的簡(jiǎn)潔性而使用調(diào)用的方法,然后再將需要的數(shù)據(jù)返回。那接著往下看:
- */
- List subMessList=null; //這個(gè)到時(shí)候存的是用分頁技術(shù)之后的要顯示的記錄
- int showCount =5; //每頁顯示的記錄數(shù)。
- int showPage = 1; //當(dāng)前顯示頁碼數(shù)。
- int size =list.size(); //所取得的數(shù)據(jù)的總條數(shù)。
- int pageCount = (size-1)/showCount + 1; //需要顯示的總頁數(shù)
- if(size
到了這里,java代碼就寫完了,不多吧加括號(hào)一共33行。接下來就要到j(luò)sp里面去顯示了。也是為了頁面的整潔和通用性,我把分頁顯示的東東放到了一個(gè)jsp里面。下面看這個(gè)jsp:
- <%@ page language="java" pageEncoding="gb18030"%>
- <div align=center>
- <br>
- <%
- String method=request.getParameter("method");
method這個(gè)參數(shù)呢,是要區(qū)別對(duì)待具體那個(gè)action的那個(gè)方法
- String action=request.getParameter("action");
action這個(gè)參數(shù)的作用,看下面就知道了
- int showPage = ((Integer)(request.getAttribute("showPage"))).intValue();
- int size = ((Integer)(request.getAttribute("size"))).intValue();
- int pageCount = ((Integer)(request.getAttribute("pageCount"))).intValue();
- int page1=showPage-1;
- int page2=showPage+1;
- int LastPage=pageCount;
- %>
- <%
- out.println("總共有"+size+"條記錄 ");
- out.println("總共有"+pageCount+"頁 ");
- out.println("當(dāng)前是第"+showPage+"頁 ");
- if(showPage > 1)
- {
- out.println("<a href='"+action+".do?method="+method+"&page=1'>***頁</a>");
- }
- else
- {
- out.println("***頁");
- }
- %>
- ?。?
- if(showPage > 1)
- {
- out.println("<a href='"+action+".do?method="+method+"&page="+page1+"'>上一頁</a>");
- }
- else
- {
- out.println("上一頁");
- }
- %>
- ?。?
- if(showPage < pageCount)
- {
- out.println("<a href='"+action+".do?method="+method+"&page="+page2+"'>下一頁</a>");
- }
- else
- {
- out.println("下一頁");
- }
- %>
- <%
- if(showPage<pageCount)
- {
- out.println("<a href='"+action+".do?method="+method+"&page="+LastPage+"'>尾頁</a>");
- }
- else
- {
- out.println("尾頁");
- }
- %>
- </div>
關(guān)于這個(gè)jsp的代碼,不用解釋太多了吧。再有就是具體的顯示頁面中,用<jsp:include page="../fenye.jsp?action=link"></jsp:include>語句將它包含到相應(yīng)為止就可以了。
【編輯推薦】