/** * 处理其他方法 * * @throws Exception * @throws * @throws InstantiationException */ @SuppressWarnings({"rawtypes", "unchecked"}) protected boolean handleOther( HttpServletRequest request, Set<SearchFilter> filterSet, Method method, DataControl dataControl, Module module) throws Exception { String[] ids = null; if (method.getName().endsWith(MANY_METHOD_SUFFIX)) { // 多对象操作方法 ids = request.getParameterValues(MANY_KEY); if (ids != null) { filterSet.add(new SearchFilter("id", Operator.IN, ids)); } } else { String id = request.getParameter("id"); if (id != null) { filterSet.add(new SearchFilter("id", Operator.EQ, id)); } else { // 截取类似/update/{id}的id String uri = request.getRequestURI(); String tmp = StringUtils.substringAfterLast(uri, "/"); Long longId = NumberUtils.toLong(tmp); if (longId != 0L) { filterSet.add(new SearchFilter("id", Operator.EQ, longId)); } } } Object clazz = Class.forName(module.getClassName()).newInstance(); // Object clazz = null; Specification spec = DynamicSpecifications.bySearchFilter(request, clazz.getClass(), filterSet); CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery criteriaQuery = builder.createQuery(clazz.getClass()); Root root = criteriaQuery.from(clazz.getClass()); Predicate predicate = spec.toPredicate(root, criteriaQuery, builder); criteriaQuery.where(predicate); List<Object> objects = em.createQuery(criteriaQuery).getResultList(); if (ids != null) { if (objects.size() == ids.length) { return true; } } else { if (objects.size() > 0) { return true; } } return false; }
private <S> Root<M> applySpecificationToCriteria(Specification<M> spec, CriteriaQuery<S> query) { Assert.notNull(query); Root root = query.from(this.entityClass); if (spec == null) { return root; } CriteriaBuilder builder = this.em.getCriteriaBuilder(); Predicate predicate = spec.toPredicate(root, query, builder); if (predicate != null) { query.where(predicate); } return root; }
@Override public <C, X> Page<C> findAll( Specification<T> spec, Class<C> clazz, Selections<T, C, X> selection, Pageable pageable) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<C> query = builder.createQuery(clazz); Root<T> root = query.from(getDomainClass()); Predicate predicate = spec.toPredicate(root, query, builder); if (predicate != null) { query.where(predicate); } query.select(builder.construct(clazz, selection.select(root, query, builder))); List<Expression<?>> groupBy = query.getGroupList(); Predicate having = query.getGroupRestriction(); if (pageable.getSort() != null) { query.orderBy(toOrders(pageable.getSort(), root, builder)); } TypedQuery<C> typeQuery = em.createQuery(query); return pageable == null ? new PageImpl<>(typeQuery.getResultList()) : readPage(typeQuery, pageable, spec, selection, groupBy, having); }