Ejemplo n.º 1
0
  /** 在HQL的后面添加分页参数定义的orderBy, 辅助函数. */
  protected String setPageRequestToHql(String hql, PageRequest pageRequest) {

    if (CollectionUtils.isEmpty(pageRequest.getSort())) {
      return hql;
    }

    StringBuilder builder = new StringBuilder(hql);
    builder.append(" order by");

    for (PageRequest.Sort orderBy : pageRequest.getSort()) {
      builder.append(String.format(" %s %s,", orderBy.getProperty(), orderBy.getDir()));
    }

    builder.deleteCharAt(builder.length() - 1);

    return builder.toString();
  }
Ejemplo n.º 2
0
  /**
   * 设置分页参数到Criteria对象,辅助函数.
   *
   * @param c Hibernate Criteria
   * @param pageRequest 分页请求参数
   * @return {@link org.hibernate.Criteria}
   */
  protected Criteria setPageRequestToCriteria(Criteria c, PageRequest pageRequest) {
    Assert.isTrue(pageRequest.getPageSize() > 0, "分页大小必须大于0");

    c.setFirstResult(pageRequest.getOffset());
    c.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.isOrderBySetted()) {
      for (PageRequest.Sort sort : pageRequest.getSort()) {
        Order order = null;
        if (sort.getDir().equals(PageRequest.Sort.ASC)) {
          order = Order.asc(sort.getProperty());
        } else {
          order = Order.desc(sort.getProperty());
        }
        c.addOrder(order);
      }
    }

    return c;
  }