Пример #1
0
  /**
   * 获取分页列表
   *
   * @param query String
   * @param args Object
   * @param pageNo int
   * @param pageSize int
   * @param count int
   * @return PageResult
   */
  public PageResult getList(String query, Object args[], int pageNo, int pageSize, int count) {
    logger.debug("query:" + query);
    PageResult pager = new PageResult();
    pager.setPageSize(pageSize);
    pager.setCurrentPageNo(pageNo);
    Session session = null;
    try {
      getHibernateTemplate().setAllowCreate(true);
      session = getSession();
      Query q = session.createQuery(query);

      if (args != null && args.length > 0) { // 设置参数
        for (int i = 0; i < args.length; i++) q.setParameter(i, args[i]);
      }
      pager.setTotalRecordCount(count); // 查询总记录数
      q.setFirstResult(pageSize * (pager.getCurrentPageNo() - 1));
      q.setMaxResults(pageSize);
      pager.setResults(q.list());

      q = null;
    } catch (HibernateException e) {
      logger.error(e);
    } finally {
      if (session != null) {
        releaseSession(session);
      }
    }
    return pager;
  }
Пример #2
0
  /**
   * @param criteria
   * @param pageNo
   * @param pageSize
   * @return
   */
  public PageResult getList(
      final DetachedCriteria detachedCriteria, final int pageNo, final int pageSize) {
    PageResult pager = new PageResult();

    Criteria criteria =
        (Criteria)
            getHibernateTemplate()
                .execute(
                    new HibernateCallback() {
                      public Object doInHibernate(Session session)
                          throws HibernateException, SQLException {
                        Criteria criteria = detachedCriteria.getExecutableCriteria(session);
                        return criteria;
                      }
                    });
    CriteriaImpl impl = (CriteriaImpl) criteria;

    Projection projection = impl.getProjection();

    List orderEntries = null;
    try {
      orderEntries = (List) MyBeanUtils.getFieldValue(impl, "orderEntries");
      MyBeanUtils.setFieldValue(impl, "orderEntries", new ArrayList());
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }

    Long iCount = (Long) criteria.setProjection(Projections.rowCount()).uniqueResult();
    int totalCount = ((iCount != null) ? iCount.intValue() : 0);
    pager.setTotalRecordCount(totalCount); // 查询总记录数
    criteria.setProjection(projection);
    if (projection == null) {
      criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
    }

    try {
      List innerOrderEntries = (List) MyBeanUtils.getFieldValue(impl, "orderEntries");
      Iterator it = orderEntries.iterator();
      while (it.hasNext()) {
        innerOrderEntries.add(it.next());
      }
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }

    criteria.setFirstResult(pageSize * (pageNo - 1));
    criteria.setMaxResults(pageSize);
    pager.setPageSize(pageSize);
    pager.setCurrentPageNo(pageNo);
    pager.setResults(criteria.list());
    return pager;
  }
Пример #3
0
 /**
  * 获取分页列表
  *
  * @param query String
  * @param pageNo int
  * @param pageSize int
  * @param count int
  * @return PageResult
  */
 public PageResult getListBySQL(String query, int pageNo, int pageSize, int count) {
   logger.debug("query:" + query);
   PageResult pager = new PageResult();
   pager.setPageSize(pageSize);
   pager.setCurrentPageNo(pageNo);
   Session session = null;
   try {
     getHibernateTemplate().setAllowCreate(true);
     session = getSession();
     Query q = session.createSQLQuery(query);
     pager.setTotalRecordCount(count); // 查询总记录数
     q.setFirstResult(pageSize * (pager.getCurrentPageNo() - 1));
     q.setMaxResults(pageSize);
     pager.setResults(q.list());
     q = null;
   } catch (HibernateException e) {
     logger.error(e);
   } finally {
     if (session != null) {
       releaseSession(session);
     }
   }
   return pager;
 }