隨筆-7  評(píng)論-24  文章-102  trackbacks-0

                  HibernateTemplate 只支持 .setMaxResults(int) 方法。
                  因此,做 Spring+Hibernate 分頁處理要使用到一個(gè)接口 org.springframework.orm.hibernate3.HibernateCallback
          來靈活操作數(shù)據(jù)庫,該接口中有一個(gè)未實(shí)現(xiàn)的方法 Object doInHibernate (Session session),用以獲得并利用 session 進(jìn)行操作(自動(dòng)創(chuàng)建、銷毀)。

          以下代碼均參考了 使用 HibernateTemplate 實(shí)現(xiàn)分頁查詢 一文。

          /**
           * 
           
          */

          package springdao;

          import hibernatedao.HibernateSessionFactory;
          import java.sql.SQLException;
          import java.util.List;
          import org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;
          import org.hibernate.HibernateException;
          import org.hibernate.Session;
          import org.springframework.orm.hibernate3.HibernateCallback;
          import org.springframework.orm.hibernate3.HibernateTemplate;



          /**
           * 通用 DAO 包
           * 
          @author kiant
           * 
          @version Sep 7, 2008
           
          */

          public class CommomsDAO {
              
          private static final Log log = LogFactory.getLog(EcOpusDAO.class);
              
          //獲得會(huì)話
              private static HibernateTemplate hibernateTemplate = new HibernateTemplate(HibernateSessionFactory.getSessionFactory());
              
              
              
          /**
               * 分頁通用方法
               * 
          @param hql  HQL查詢語句
               * 
          @param offset    起始記錄下標(biāo)
               * 
          @param lengh        讀取記錄數(shù)
               * 
          @return            List 結(jié)果集
               
          */

              
          public static List getListForPage(final String hql, final int offset, final int lengh) {
                  log.debug(
          "finding ListForPage");
                  
          try {
                      List list 
          = hibernateTemplate.executeFind(new HibernateCallback(){

                          
          public Object doInHibernate(Session session)
                                  
          throws HibernateException, SQLException {
                              List list2 
          = session.createQuery(hql)
                                      .setFirstResult(offset)
                                      .setMaxResults(lengh)
                                      .list();                    
                              
          return list2;
                          }
          }
          );
                      
          return list;
                  }
           catch (RuntimeException re) {
                      log.error(
          "find ListForPage failed", re);
                      
          throw re;
                  }

              }

          }



          ps.
          也可以通過:
                  this.getSession();
                  this.getHibernateTemplate().getSessionFactory().openSession();
          分別獲取 session 進(jìn)行 createQuery()等操作。
          但是這種做法,需要自己去手動(dòng)關(guān)閉session的。所以你需要配置openSessioninview,不推薦使用!
          posted on 2008-09-07 11:25 黃小二 閱讀(3010) 評(píng)論(1)  編輯  收藏 所屬分類: S/S2SH

          評(píng)論:
          # re: 使用 HibernateTemplate 實(shí)現(xiàn)分頁查詢 (HibernateCallback接口) 2014-03-03 22:32 | redcoatjk
          可以強(qiáng)制加載后 close session.
          --------
          此外直接用HibernateTemplate.getSessionFactory.getCurrentSession
          應(yīng)該是可以托管給spring管理的.  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 金阳县| 博乐市| 荆州市| 晋城| 鸡西市| 原阳县| 大兴区| 永修县| 青田县| 定边县| 平安县| 鹤庆县| 台江县| 大英县| 新乡市| 建始县| 阜阳市| 射阳县| 交口县| 丹凤县| 麻阳| 南阳市| 吉林市| 霸州市| 重庆市| 海南省| 北海市| 三门峡市| 昌黎县| 祥云县| 山阴县| 武平县| 荥经县| 万全县| 长沙市| 夹江县| 保山市| 普安县| 武胜县| 光山县| 米易县|