Spring+hibernate分页查询

/**
     * TOP查询
     *  @param  sql String
     *  @param  top int
     *  @return  List
      */
     public  List findTop(String sql,  int  top) {
      HibernateTemplate ht  =   this .getHibernateTemplate();(来源 www.iocblog.net)
      ht.setMaxResults(top);
       return  ht.find(sql);
    }

     /**
     * 分页查询
     *  @param  sql String
     *  @param  firstRow int
     *  @param  maxRow int
     *  @return  List
      */
     public  List findPage( final  String sql, final   int  firstRow, final   int  maxRow) {
       return   this .getHibernateTemplate().executeFind( new  HibernateCallback(){
             public  Object doInHibernate(Session session)  throws  SQLException,
                    HibernateException {
               Query q  =  session.createQuery(sql);
               q.setFirstResult(firstRow);
               q.setMaxResults(maxRow);
                return  q.list();
               }
        });     
    }

模板实现分页:
public   List find(  final   String hsql,   final     int   firstRow,   final     int   maxRow)   throws         Exception {
   return  getHibernateTemplate().executeFind( new  HibernateCallback() {
     public  Object doInHibernate(Session s)  throws  HibernateException, SQLException {
           Query query  =  s.createQuery(hsql);
           query.setFirstResult(firstRow);
           query.setMaxResults(maxRow);
           List list  =  query.list();
           return  list;
           }
     });
}


文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。