/** * 描述:更近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(); }
/** * 多条件查询 * * @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(); }