ssh整合dao层的分页方法

    技术2022-05-19  25

     

    1.方法一

     /**  * 使用hql 语句进行分页查询操作  * @param hql 需要查询的hql语句  * @param values 如果hql有多个个参数需要传入,values就是传入的参数数组  * @param offset 第一条记录索引  * @param pageSize 每页需要显示的记录数  * @return 当前页的所有记录  */ public List findByPage(final String hql, final Object[] values,   final int offset, final int pageSize) {

      List list = getHibernateTemplate().executeFind(new HibernateCallback()   {    public Object doInHibernate(Session session)     throws HibernateException, SQLException    {     Query query = session.createQuery(hql);     for (int i = 0 ; i < values.length ; i++)     {      query.setParameter( i, values[i]);     }     List result = query.setFirstResult(offset)                     .setMaxResults(pageSize)            .list();     return result;    }   });  return list; }}

     

     

     

     

     

    2.方法二

    public Page<OpProductCategory> listRootCategorys(int start, int pageSize) {

    String hql = "from OpProductCategory p where p.parentOpProductCategory=null";

    Session session = SessionFactoryUtils.getSession(getHibernateTemplate().getSessionFactory(), true);

    session.beginTransaction();

    Query query = session.createQuery(hql);

    //先得到总的记录数

    int totalRows = query.list().size();

    //得到当页的记录

    query.setFirstResult(start);

    query.setMaxResults(pageSize);

    List<OpProductCategory> categorys= query.list();

    session.getTransaction().commit();

     

    //构造一个Page对象

    Page<OpProductCategory> page = new Page<OpProductCategory>(start,totalRows,pageSize,categorys);

     

    return page;

    }


    最新回复(0)