Spring 页面查询

来源:互联网 发布:excel数据分类统计分析 编辑:程序博客网 时间:2024/05/16 19:29

/**

     * TOP查询

     *  @param  sql String

     *  @param  top int

     *  @return  List

      */

     public  List findTop(String sql,  int  top) {

      HibernateTemplate ht  =   this .getHibernateTemplate();

      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; 

           } 

     }); 

}

原创粉丝点击