Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增强基于SearchFilter的动态查询功能 #479

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.*;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;
Expand All @@ -33,11 +29,27 @@ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuild
// nested path translate, 如Task的名为"user.name"的filedName, 转换为Task.user.name属性
String[] names = StringUtils.split(filter.fieldName, ".");
Path expression = root.get(names[0]);
for (int i = 1; i < names.length; i++) {
expression = expression.get(names[i]);
}
for (int i = 1; i < names.length; i++) {
if (Collection.class.isAssignableFrom(expression.getJavaType())) {
Join parentJoin = null;
for (int j = 0; j < i; j++) {
if (j == 0) {
parentJoin = root.join(names[j]);
} else {
parentJoin = parentJoin.join(names[j]);
}
}
expression = parentJoin.get(names[i]);
} else {
expression = expression.get(names[i]);
}
}

if (expression.getJavaType().isEnum()) {
filter.value = Enum.valueOf(expression.getJavaType(), filter.value.toString());
}

// logic operator
// logic operator
switch (filter.operator) {
case EQ:
predicates.add(builder.equal(expression, filter.value));
Expand Down