自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

淺談JSP下的Hibernate分頁技術(shù)

開發(fā) 后端
分頁在任何系統(tǒng)中都是非常頭疼的事情,有的數(shù)據(jù)庫在語法上支持分頁,而有的數(shù)據(jù)庫則需要使用可滾動游標來實現(xiàn),并且在不支持可滾動游標的系統(tǒng)上只能使用單向游標逐步接近要取得的數(shù)據(jù)。Hibernate提供了一個支持跨系統(tǒng)的分頁機制,這樣無論底層是什么樣的數(shù)據(jù)庫都能用統(tǒng)一的接口進行分頁操作。本文講述的是JSP下的Hibernate分頁技術(shù)。

這是我知道的代碼最少且最簡潔的一種Hibernate分頁技術(shù)了,自己懶,所以拼命減少代碼量,呵呵。下面用人能看得懂的語言細說一下,關(guān)于Hibernate的分頁技術(shù),無外乎兩種:

1. 從數(shù)據(jù)庫中取得記錄,在內(nèi)存中再劃分。但如果遇到記錄數(shù)很大的時候效率很成問題。

2. 采用Hibernate的物理分頁,每次只是取一頁。從客戶端傳進來的是第幾頁和每頁多少條記錄,要首先查詢符合記錄的總記錄數(shù),再根據(jù)總記錄數(shù)和當前頁,每頁記錄數(shù)可以算出要取的是數(shù)據(jù)庫中的第幾條記錄。但2次查詢不可避免了。

所以總結(jié)了兩種方式的優(yōu)劣,如果數(shù)據(jù)量不是非常大的話(百萬以上),采用***種方法,否則可選擇第二種。

由于我要操作的數(shù)據(jù)庫信息量沒有達到大的標準,所以我采用了***種方法,下面細說。

首先看一下我的一個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");
}

很簡單的代碼,就是查詢數(shù)據(jù),扔到一個List里面,然后setAttribute,再在jsp頁面顯示就可以了。

接下來談分頁,考慮到了簡潔性和通用性,我把分頁的代碼單獨封裝到了一個類里面去,下面看看這個類:

public class Fenye {
 public List fenye(ActionMapping mapping, ActionForm form,
     HttpServletRequest request, HttpServletResponse response){
 
    List list=(ArrayList) request.getAttribute("list");
/*

這里有人可能就看不懂了,為什么要帶這些參數(shù)?因為我上面的action方法是分頁之前的方法,所以不能看出來。

下面貼一下用到分頁之后的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");
  }

和上面的一比較,其實就多了兩行代碼,為的就是保持頁面的簡潔性而使用調(diào)用的方法,然后再將需要的數(shù)據(jù)返回。

那接著往下看:

*/
  List subMessList=null;  //這個到時候存的是用分頁技術(shù)之后的要顯示的記錄
    int showCount =5;     //每頁顯示的記錄數(shù)。
    int showPage = 1;     //當前顯示頁碼數(shù)。
    int size =list.size();     //所取得的數(shù)據(jù)的總條數(shù)。
    int pageCount = (size-1)/showCount + 1;  //需要顯示的總頁數(shù)
    if(size

到了這里,java代碼就寫完了,不多吧加括號一共33行。接下來就要到jsp里面去顯示了。也是為了頁面的整潔和通用性,我把分頁顯示的東東放到了一個jsp里面。下面看這個jsp:

<%@ page language="java" pageEncoding="gb18030"%>
 <div align=center>
  <br>
 
     <%
     String method=request.getParameter("method");

method這個參數(shù)呢,是要區(qū)別對待具體那個action的那個方法

String action=request.getParameter("action");

action這個參數(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+"條記錄&nbsp");

         out.println("總共有"+pageCount+"頁&nbsp");
           out.println("當前是第"+showPage+"頁&nbsp");
               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)于這個jsp的代碼,不用解釋太多了吧。再有就是具體的顯示頁面中,用<jsp:include page="../fenye.jsp?action=link"></jsp:include>語句將它包含到相應為止就可以了。

【編輯推薦】

  1. 高性能、高彈性JSP和Servlet性能優(yōu)化
  2. JSTL和EL開發(fā)JSP頁面的優(yōu)勢及實現(xiàn)
  3. Tomcat下JSP經(jīng)典配置實例
責任編輯:楊鵬飛 來源: 天下無藍的空間
相關(guān)推薦

2009-09-22 16:49:42

Hibernate分頁

2009-06-05 09:52:25

struts分頁Hibernate

2009-06-15 13:46:00

netbeans配置hibernate

2009-09-21 13:05:18

Hibernate u

2009-09-23 17:07:31

Hibernate C

2009-09-28 15:47:59

Hibernate O

2009-09-22 10:09:21

Hibernate S

2009-09-25 10:53:40

Hibernate S

2009-09-29 10:46:58

Hibernate領(lǐng)域

2009-07-01 10:01:33

JSP分頁查詢MySQL數(shù)據(jù)庫

2009-09-21 18:13:11

Hibernate S

2009-09-23 10:19:08

Hibernate分頁

2009-07-06 15:58:11

JSP程序

2009-09-28 13:43:28

使用Hibernate

2009-07-20 16:18:54

iBatis分頁Hibernate式的

2009-09-23 14:40:17

Hibernate F

2009-07-06 17:46:41

JSP網(wǎng)站

2009-09-22 13:41:10

直接使用Hiberna

2009-09-28 13:39:01

Hibernate工作

2009-09-27 10:02:29

定制Hibernate
點贊
收藏

51CTO技術(shù)棧公眾號