日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
淺談JSP下的Hibernate分頁技術(shù)

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

目前創(chuàng)新互聯(lián)公司已為上千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、句容網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

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");
}

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

接下來談分頁,考慮到了簡潔性和通用性,我把分頁的代碼單獨(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í)就多了兩行代碼,為的就是保持頁面的簡潔性而使用調(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)為止就可以了。


網(wǎng)頁標(biāo)題:淺談JSP下的Hibernate分頁技術(shù)
新聞來源:http://www.5511xx.com/article/dpgcioj.html