java 分页方法

来源:互联网 发布:linux more 下一页 编辑:程序博客网 时间:2024/06/05 14:08
分页查询的两种方式:
    第一种:使用SessionFactory .openSession();方法,此方法不会创建的session会和绑定到当前线程,创建的线程不会在事务回滚或事物提交后自动关闭,必须手动关闭。
        
        import org.hibernate.Query;
        import org.hibernate.Session;
        import org.springframework.orm.hibernate3.HibernateTemplate;
        /**
            * @Description: 审批已通过报销单请求,分页查询
            * @param fid 查询条件
            * @param m 第几条数据开始
            * @param n 展示几条数据
            * @return List<UserReimburse>
            * @throws
          */
        @ SuppressWarnings("unchecked")
        public List<UserReimburse> findApprovaPass(String fid, int m, int n) {

            List<UserReimburse> result = null;
            try {
                SessionFactory sf = hTemplate.getSessionFactory();
                Session session = sf.openSession();

                String hql = "from UserReimburse u where u.userAccount=:fid and (reimburseState=3 or reimburseState=2) order by u.reimburseResultDate desc";
                System.out.println("=======================>>>>>>>" + hql);
                Query query = session.createQuery(hql);
                query.setParameter("fid", fid);
                query.setFirstResult(m);
                query.setMaxResults(n);
                result = query.list();
                System.out.println("result = " + result);

                session.flush();
                session.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
       }

    第二种:使用SessionFactory .getCurrentSession();方法,此方法会创建的session会和绑定到当前线程,创建的线程会在事务回滚或事物提交后自动关闭。
        使用此方法Spring中applicationContext.xml配置文件要进行如下设置:
        <!-- JDBC事务管理器,根据你的情况使用不同的事务管理器,如果工程中有Hibernate,就用Hibernate的事务管理器  -->
        <beans
            xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:p="http://www.springframework.org/schema/p"
            xmlns:context="http://www.springframework.org/schema/context"
            xmlns:aop="http://www.springframework.org/schema/aop"
            xmlns:tx="http://www.springframework.org/schema/tx"
            xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/aop
             http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
             http://www.springframework.org/schema/tx
             http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="SessionFactory" />
        </bean>
        <!-- 用注解来实现事务管理  -->
        <tx:annotation-driven transaction-manager="transactionManager" />

        
        import org.hibernate.Query;
        import org.hibernate.Session;
        import org.springframework.orm.hibernate3.HibernateTemplate;
        import org.springframework.transaction.annotation.Transactional;
        
        /**
            * 分页查询
            *
            * @param message 查询条件
            * @param pageNo 第几条数据开始
            * @param pageSize 展示几条数据
            * 
            * @return  List<UserReimburse>
            */
         @SuppressWarnings("unchecked")
         @Transactional(rollbackFor=Exception.class)
         public List<UserReimburse> findPageByExcemple(String message, int pageNo, int pageSize) throws Exception{
         List<UserReimburse> listReimburse = null;
         //String hql = "from UserReimburse u where u.userAccount='YBWADC' and (reimburseState=3 or reimburseState=2) order by u.reimburseResultDate desc";
         String hql = "from UserReimburse u where u.userAccount=:fid and (reimburseState=3 or reimburseState=2) order by u.reimburseResultDate desc";
         try {
             System.out.println("hql >>>> " + hql);
             Session session = hbTemplate.getSessionFactory().getCurrentSession();
             Query query = session.createQuery(hql);
              query.setParameter("fid", message);
              query.setFirstResult(pageNo); // 第几条数据
              query.setMaxResults(pageSize); // 展示几条数据
              System.out.println("分页查询结果 = " + query.list());
              listReimburse = query.list();
              if (null != listReimburse) {
                    return listReimburse;
               }
           } catch (Exception e) {
               e.printStackTrace();
               throw e;
          }
           return null;
        }
   
0 0
原创粉丝点击