Пример #1
0
 /**
  * 描述:更近sc统计相关信息条数 <br>
  * 创建时间:2013-5-11 下午11:51:02
  *
  * @author liyang
  * @param lockMode
  * @param cacheName
  * @param sc
  * @return
  */
 public Object statBySearchCriterion(
     final LockModeType lockMode, final ConstantsEnum.EHCACHE cacheName, SearchCriterion sc) {
   if (sc == null) {
     sc = new SearchCriterion();
   }
   Map<String, Object> map = sc.getFilterMap();
   Query query = entityManager.createQuery("select count(*) " + this.getJPQL(sc));
   if (map != null && !map.isEmpty()) {
     for (String str : map.keySet()) {
       if (str.split("\\.").length > 1) {
         query.setParameter(str.split("\\.")[1], map.get(str));
       } else {
         query.setParameter(str, map.get(str));
       }
     }
   }
   map = sc.getFilterValue();
   if (map != null && !map.isEmpty()) {
     for (String str : map.keySet()) {
       if (str.split("\\.").length > 1) {
         query.setParameter(str.split("\\.")[1], map.get(str));
       } else {
         query.setParameter(str, map.get(str));
       }
     }
   }
   if (lockMode != null) {
     query.setLockMode(lockMode);
   }
   if (cacheName != null) {
     query.setHint("org.hibernate.cacheable", true);
     query.setHint("org.hibernate.cacheRegion", cacheName.getStr());
   }
   return query.getSingleResult();
 }
Пример #2
0
  /**
   * 多条件查询
   *
   * @param lockMode
   * @param cacheName 二级缓存的名字,ehcache定义的
   * @param sc 多条件组合查询类
   * @return 返回实体类集合
   */
  @SuppressWarnings("unchecked")
  public List<T> findBySearchCriterion(
      final LockModeType lockMode, final ConstantsEnum.EHCACHE cacheName, SearchCriterion sc) {
    // 拼装查询条件
    if (sc == null) {
      sc = new SearchCriterion();
    }
    Map<String, Object> map = sc.getFilterMap();
    Query query = entityManager.createQuery(" select t  " + this.getJPQL(sc));
    if (map != null && !map.isEmpty()) {
      for (String str : map.keySet()) {
        if (str.split("\\.").length > 1) {
          query.setParameter(str.split("\\.")[1], map.get(str));
        } else {
          query.setParameter(str, map.get(str));
        }
      }
    }
    map = sc.getFilterValue();
    if (map != null && !map.isEmpty()) {
      for (String str : map.keySet()) {
        if (str.split("\\.").length > 1) {
          query.setParameter(str.split("\\.")[1], map.get(str));
        } else {
          query.setParameter(str, map.get(str));
        }
      }
    }

    if (sc.getPager() != null) {
      int rowStartIdx = Math.max(0, sc.getPager().getStartRow());
      if (rowStartIdx > 0) {
        query.setFirstResult(rowStartIdx);
      }

      int rowCount = Math.max(0, sc.getPager().getPageSize());
      if (rowCount > 0) {
        query.setMaxResults(rowCount);
      }
    }
    if (lockMode != null) {
      query.setLockMode(lockMode);
    }
    if (cacheName != null) {
      query.setHint("org.hibernate.cacheable", true);
      query.setHint("org.hibernate.cacheRegion", cacheName.getStr());
    }
    return query.getResultList();
  }
Пример #3
0
  private String getJPQL(SearchCriterion sc) {

    boolean isWhere = false;
    StringBuilder sb = new StringBuilder();
    sb.append(" from ");
    sb.append(clazz.getName());
    sb.append(" t ");
    if (StringUtils.isNotBlank(sc.getRelevanceSearch())) {
      sb.append(sc.getRelevanceSearch());
    }
    if (!StringUtils.isEmpty(sc.getFilter())) {
      isWhere = true;
      sb.append(" where ");
      sb.append(sc.getFilter());
    }
    Map<String, Object> map = sc.getFilterMap();
    if (map != null && !map.isEmpty()) {
      if (!isWhere) {
        sb.append(" where ");
      } else {
        sb.append("   and ");
      }
      for (String s : map.keySet()) {
        sb.append(s);
        sb.append(" = :");
        if (s.split("\\.").length > 1) {
          sb.append(s.split("\\.")[1]);
        } else {
          sb.append(s);
        }
        sb.append("   and ");
      }
      sb.replace(sb.length() - 6, sb.length(), "");
    }
    // 拼装排序条件
    List<SortCriterion> sortList = sc.getSortCriteria();
    if (sortList != null && !sortList.isEmpty()) {
      sb.append(" order by ");
      for (SortCriterion s : sortList) {
        sb.append(s.getSortField());
        sb.append(" ");
        sb.append(s.getSortType());
        sb.append(",");
      }
      sb.replace(sb.length() - 1, sb.length(), "");
    }
    return sb.toString();
  }