<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    JAVA—咖啡館

    ——?dú)g迎訪問rogerfan的博客,常來《JAVA——咖啡館》坐坐,喝杯濃香的咖啡,彼此探討一下JAVA技術(shù),交流工作經(jīng)驗(yàn),分享JAVA帶來的快樂!本網(wǎng)站部分轉(zhuǎn)載文章,如果有版權(quán)問題請與我聯(lián)系。

    BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
      447 Posts :: 145 Stories :: 368 Comments :: 0 Trackbacks
    名聲顯赫而招搖的數(shù)據(jù)持久層框架Hibernate,通過query.setFirstResult和query.setMaxResult來實(shí)現(xiàn)了對數(shù)據(jù)的分頁,這個(gè)分頁的實(shí)質(zhì)在SqlServer中是TOP N的方法,Oracle是rownum<n方法。即直接通過SQL語句,來得到當(dāng)前頁所需的數(shù)據(jù)。

    但是,Hibernate分頁,并不能得到頁腳,所以尚不通用。承接Jpage分頁的方便性,寫了這版分頁,以方便已與群眾。

    下面,就是在Hibernate下的通用分頁,屬于Jpage分頁的第三項(xiàng)功能。思路是定義一個(gè)Dao類,讓其它數(shù)據(jù)庫的dao都繼承這個(gè)Dao類。


    Dao類源文件:


     

    package com.xdf.dao;

    import java.io.*;
    import java.sql.*;
    import java.util.*;
    import javax.servlet.http.*;
    import org.hibernate.*;
    import org.hibernate.criterion.Projections;

    import com.xdf.hibernate.HibernateSessionFactory;

    public class Dao {

     HttpServletRequest request;

     HttpServletResponse response;

     Session session;

     //定義List集合
     private List list;

     public int intCountic;

     public int PageSize;

     public int intPageCount;

     public int intPage;

     public String nowPage;

     public String HttpFile;
     


     //取得網(wǎng)址是的參數(shù)
     String PageParameter;

     public Dao(HttpServletRequest request, HttpServletResponse response) {

      this.request = request;
      this.response = response;
     }

     /*
      *******得到Session**********
      */
     public Session getSession() {
      session = HibernateSessionFactory.getSession();
      return session;

     }

     //添加數(shù)據(jù)
     public void add(Object obj) {
     }

     //修改數(shù)據(jù)
     public void mod(Object obj) {
     }

     //普通查詢
     public List list() {
      return null;
     }

     
     
     public List jlist(Object query, String countsql)
     {

      
      Query q=null;
      Criteria cri=null;
      List li=null;
       
     
      boolean bool=false;;
      
      //得到記錄總數(shù)
            if(query instanceof Query) {
             q=(Query)query;
             bool=true;
       intCountic = getCountJsql(countsql);
       
      }
           
            else if(query instanceof Criteria) {
             
             cri=(Criteria)query;
             li=cri.list();
             
          
             if(li.size()==0)
              intCountic=0;
             else
              intCountic=li.size();
           
      }
            else
            {
             
             System.out.println("出錯(cuò)提示:參數(shù)不正確");
             return null;
            }
           
        
           
      try
      {
      //每頁記錄數(shù)
          if (PageSize == 0)
       PageSize = 20;

      //獲得url,如 /user/china.jsp
      HttpFile = request.getRequestURI();

      //獲得當(dāng)前頁數(shù)
      nowPage = request.getParameter("pages");

      //取得網(wǎng)址是的參數(shù)
      PageParameter = this.urlPath(request);

      //如果沒有pages參數(shù),則讓頁數(shù)為1
      if (nowPage == null) {
       intPage = 1;
      }

      else {
       intPage = Integer.parseInt(nowPage);
       if (intPage < 1) {
        intPage = 1;
       }
      }

      

      //得到分頁總數(shù)
      intPageCount = ((intCountic + PageSize) - 1) / PageSize;

      if (intPage > intPageCount) {
       intPage = intPageCount;
      }
        
    //  得到list集合
      
       if(bool)
       list = q.setFirstResult((intPage - 1) * PageSize).setMaxResults(PageSize).list();    
       else
          list =cri.setFirstResult((intPage - 1) * PageSize).setMaxResults(PageSize).list(); 
      
     
      
      }
      catch(Exception ex)
      {
       ex.printStackTrace();
      }
      finally
      {
         
      }
      
      //將分頁和頁腳傳到request中
            request.setAttribute("list", list);
            request.setAttribute("foot", PageFooter());
      return list;
     }
     
     
     
     
     
     
     public List jlist(Query query)
     {
      
        String[] str = query.getQueryString().split("from");  
        String countsql ="select count(*) from "+str[1].trim();  
        return jlist(query,countsql);  
      
     }
     
     public List jlist(Criteria query)
     {
      
      return jlist(query,"");
      
      
     }
     
     
     
     

     //匯總語句,得到記錄總數(shù)
     public int getCountJsql(String countsql) {
      Query q = getSession().createQuery(countsql);
     
      List cc = q.list();
      Integer a = (Integer) cc.get(0);
      return a.intValue();
      
     
     }

     //頁腳顯示
     /**
      * 顯示分頁內(nèi)容
      * @return
      */
     public String PageFooter() {

     
      if (list.size() <= 0) {
           return "<span style=\"color:#FF3300;font-size:14px;\">對不起,暫無記錄!</span>";
            }
      
      String style = "<style> \n";
      style += ".page {color: #333333; background-color: #F3F3F3; height: 18px;width: 36px;";
      style += "border: 1px solid #333333;margin-right: 1px; margin-left: 1px;} \n";
      style += " .fytd {font-size: 12px; color: #333333;}\n";
      style += ".fy:link {color: #333333;text-decoration: underline;font-size: 12px;} \n";
      style += ".fy:visited {color: #333333;text-decoration: underline;font-size: 12px;} \n";
      style += ".fy:hover{color: #000000;text-decoration: none;border: 1px solid #999999;";
      style += "background-position: center center;font-size: 12px; background-color: #FFFFFF;} \n";
      style += ".fy:active {color: #000000;text-decoration: none;border: 1px solid #999999;";
      style += "background-position: center center;padding: 1px 1px 0px;font-size: 12px;background-color: #FFFFFF;} \n";
      style += "</style> \n";

      String str = "";
      int prev = intPage - 1;
      int next = intPage + 1;
      str = str
        + "<table width=100% border=0 cellspacing=0 cellpadding=1 class=fytd>";
      str = str + "<tr><td width=45%>共計(jì):<font color=#FF3300>[" + intCountic
        + "]</font>條 <font color=#FF3300> [" + intPageCount
        + "]</font>頁";
      str = str + " 第<font color=#FF3300>[" + getIntPage()
        + "]</font>頁</td><td width=55%>";
      str = str
        + "<table width=275 border=0 align=right cellpadding=0 cellspacing=0 class=fytd>";
      //*******
      String pstr = HttpFile + "?" + PageParameter.replace("&pages=", "");
      pstr = pstr.replace("?pages=", "");
      str = str + "<form action=" + pstr
        + " name=formin method=post><tr><td width=195>";
      if (intPage > 1) {
       str = str + " <A href=../../" + HttpFile + "?" + PageParameter + "1"
         + " class=fy>[首頁]</A> ";
      } else {
       str = str + " [首頁] ";
      }
      if (intPage > 1) {
       str = str + " <A href=../../" + HttpFile + "?" + PageParameter + prev
         + " class=fy>[上一頁]</A> ";
      } else {
       str = str + " [上一頁] ";
      }
      if (intPage < intPageCount) {
       str = str + " <A href=../../" + HttpFile + "?" + PageParameter + next
         + " class=fy>[下一頁]</A> ";
      } else {
       str = str + " [下一頁] ";
      }
      if (intPageCount > 1 && intPage != intPageCount) {
       str = str + " <A href=../../" + HttpFile + "?" + PageParameter
         + intPageCount + " class=fy>[尾頁]</A>";
      } else {
       str = str + " [尾頁]";
      }
      str = str
        + "</td><td width=80 align=right><input name=pages type=text class=page value="
        + intPage
        + " size=3 maxlength=5 onkeyup=javascript:value=value.replace(/[^\\d]/g,'')>";
      str = str
        + "<input name=Submit2 type=submit class=page value=轉(zhuǎn)到></td></tr></form></table>";
      str+="</td></tr></table>";
      return style + str;
     }

     /**
      * 對有參數(shù)的網(wǎng)址進(jìn)行改造。。
      * 如 index.jsp?id=23&class=23
      */

     public String urlPath(HttpServletRequest request) {
      String path = "";
      String pagepath = "pages=";
      String url = request.getQueryString();

      //如果無參數(shù)
      if (url == null || url.equals("")) {
       return pagepath;
      }

      List lista = new ArrayList();
      StringTokenizer ss = new StringTokenizer(url, "&");

      while (ss.hasMoreTokens()) {
       String s = ss.nextToken();
       if (s.indexOf("pages") == -1)
        lista.add(s);
      }

      for (int i = 0; i < lista.size(); i++) {
       String param = "";
       try {
        param = new String(lista.get(i).toString().getBytes(
          "iso-8859-1"), "gb2312");
       } catch (UnsupportedEncodingException e) {

        e.printStackTrace();
       }
       path += param + "&";
      }

      return path + pagepath;

     }

     public int getIntCountic() {
      return intCountic;
     }

     public void setIntCountic(int intCountic) {
      this.intCountic = intCountic;
     }

     public int getIntPage() {
      return intPage;
     }

     public void setIntPage(int intPage) {
      this.intPage = intPage;
     }

     public int getIntPageCount() {
      return intPageCount;
     }

     public void setIntPageCount(int intPageCount) {
      this.intPageCount = intPageCount;
     }

     public String getNowPage() {
      return nowPage;
     }

     public void setNowPage(String nowPage) {
      this.nowPage = nowPage;
     }

     public int getPageSize() {
      return PageSize;
     }

     public void setPageSize(int pageSize) {
      PageSize = pageSize;
     }
    }

    /**
     * 注意事項(xiàng):
     * 1、可傳遞Query或Critera參數(shù)
     * 2、傳遞Query對象時(shí),不能使用包括?號的HQL語句。
     * 3、使用Critera時(shí),是將所有數(shù)據(jù)讀到List中,從而獲得記錄數(shù),經(jīng)測試5萬條記錄內(nèi),差別不明顯
      *
     */


    產(chǎn)品dao類(具體dao):


     

     

    package com.xdf.dao;
    import java.util.List;
    import javax.servlet.http.*;
    import org.hibernate.*;
    import org.hibernate.Transaction;
    import com.xdf.bean.PInfo;
    import com.xdf.hibernate.HibernateSessionFactory;
    import com.xdf.struts.form.*;
    import javax.servlet.http.*;


    public class PinfoDao extends Dao {
     
     public PinfoDao(HttpServletRequest request,HttpServletResponse response)
     {
      super(request,response);
     }
     

     //添加產(chǎn)品  
     public void add(Object po) { }
     //修改產(chǎn)品  
     public void mod(Object po) { }

      //分頁查詢
      public List list() {  
      Query query =getSession().createQuery("from PInfo");

      //想使用分頁,將Query作為參數(shù),傳給jlist方法就可以了,這時(shí)就可以得到列表和頁腳。
      //HQL查詢、條件查詢都返回Query對象,所以都可以。
       List li=jlist(query);
       //或者 List li=jlist(query,PInfo);
      return li;  
      }


     //匯總查詢
    public List count()
    {
    return null;
    }

      
    }



    3、Struts的Action調(diào)用dao:

     
    public ActionForward execute(ActionMapping mapping, ActionForm form,
       HttpServletRequest request, HttpServletResponse response) {
      System.out.println("aabbcc");
      PinfoDao dao = new PinfoDao(request,response);
      dao.list();
      return mapping.findForward("list");
      
     }



    4、在JSP中顯示:這里采用的是DIV布局

     
      <ul>
       <li>產(chǎn)品名稱</li>
       <li>產(chǎn)品類別</li>
       <li>產(chǎn)品價(jià)格</li>
       <li>操    作</li>
       </ul>
      
     
       <logic:iterate id="da" name="list">
        <ul>
       <li><bean:write name="da" property="PName" /></li>
       <li><bean:write name="da" property="PType" /></li>
       <li><bean:write name="da" property="PPrice" /></li>
       <li><a href="prod.do id=<bean:write name="da" property="PId" />">刪除</a></li>
       </ul>
       </logic:iterate>
      
       <ul>
       <li>
        <bean:write name="foot" filter="false"></bean:write>
       </li>
       </ul>




    示例下載:
    upload/2007_05/07051911442616.rar



    也就是說,你把Dao類,拷過去,讓你自己的具體的dao繼承這個(gè)類,就可以了。
    posted on 2007-11-01 21:32 rogerfan 閱讀(1236) 評論(2)  編輯  收藏 所屬分類: 【Java知識】【開源技術(shù)】

    Feedback

    # re: Jpage分頁——hibernate分頁[未登錄] 2007-11-26 16:44 GoKu
    string的+=操作太多了,建議用stringbulider,連接字符串性能差距很大  回復(fù)  更多評論
      

    # re: Jpage分頁——hibernate分頁 2008-06-11 19:34 大五
    呵呵,樓上的朋友言之不無道理。。
    但對于現(xiàn)在硬件,個(gè)人電腦大多是1G內(nèi)存了,這點(diǎn)差別不足以論。
      回復(fù)  更多評論
      

    主站蜘蛛池模板: 91在线视频免费播放| 亚洲中文字幕无码久久2020| 免费人成年轻人电影| 日韩a级毛片免费视频| 免费鲁丝片一级在线观看| 野花高清在线电影观看免费视频 | 亚洲AV综合色一区二区三区| 国产亚洲精品高清在线| 亚洲成人影院在线观看| 亚洲成a人片在线观看日本麻豆| 又大又硬又爽免费视频| 亚洲免费日韩无码系列| 亚洲精品成人在线| 亚洲午夜福利717| 综合亚洲伊人午夜网 | 卡1卡2卡3卡4卡5免费视频| 成人影片麻豆国产影片免费观看 | 国产va免费观看| 国产福利免费视频| 中文在线日本免费永久18近| 成人久久免费网站| 在线看片免费人成视久网| 色老头永久免费网站| 野花高清在线观看免费3中文 | 中文字幕在线成人免费看| a级大片免费观看| 91精品导航在线网址免费| h在线观看视频免费网站| 最近2019中文字幕免费看最新| 午夜视频免费观看| 国产a不卡片精品免费观看 | 日本黄网站动漫视频免费| 免费观看的毛片大全| 午夜高清免费在线观看| 久久久久亚洲精品中文字幕| 亚洲视频精品在线| 亚洲日本va一区二区三区| 免费精品视频在线| 免费一级毛片无毒不卡| 香蕉97超级碰碰碰免费公| 国产成人3p视频免费观看|