春季规范-谓词的合取 [英] Spring Specification - conjunction of Predicates
问题描述
我需要一个将过滤器参数和构建查询的函数。我有4个参数,因此,如果我尝试针对每种条件实施查询,都必须编写16个(2 ^ 4)
实现-这不是一个好主意。
I need a function that will be filter parameters and build query. I have 4 parameters therefore if I would try to implement query for each condition I would have to write 16 (2^4)
implementations - it's not good idea.
我尝试使用Spring Data JPA中的接口 Specification
来改进代码,但无法创建谓词的合体。
I try to improve my code with interface Specification
from Spring Data JPA but I cannot create conjunction of predicates.
规范的实现
界面:
public class UserSpecification implements Specification<User> {
private final UserSearchCriteria criteria;
public UserSpecification(UserSearchCriteria criteria) {
this.criteria = criteria;
}
@Override
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
final List<Predicate> predicates = new ArrayList<>();
if (criteria.getName() != null) {
final Predicate name = builder.equal(root.<String>get("name"), criteria.getName());
predicates.add(name);
} else if (criteria.getSurname() != null) {
final Predicate surname = builder.equal(root.<String>get("surname"), criteria.getSurname());
predicates.add(surname);
} else if (criteria.getCity() != null) {
final Predicate city = builder.equal(root.<String>get("city"), criteria.getCity());
predicates.add(city);
} else if (criteria.getCountry() != null) {
final Predicate country = builder.equal(root.<String>get("country"), criteria.getCountry());
predicates.add(country);
}
return builder.and(predicates.toArray(new Predicate[predicates.size()]));
}
}
测试不起作用:
@Test
@Sql(
scripts = "classpath:sql/specification.sql",
executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD
)
public void specificationTest() {
// given
final UserSearchCriteria criteria = UserSearchCriteria.builder()
.name("john")
.surname("smith")
.build();
final UserSpecification specification = new UserSpecification(criteria);
// when
final List<User> result = userRepository.findAll(specification);
userRepository.flush();
// then
assertThat(result).hasSize(3);
}
在测试之前,我插入了以下用户数据库:
Before test I insert to database following users:
INSERT INTO users (id, name, surname, city, country) VALUES (1, 'john', 'smith', null, null);
INSERT INTO users (id, name, surname, city, country) VALUES (2, 'john', 'smith', null, null);
INSERT INTO users (id, name, surname, city, country) VALUES (3, 'john', 'smith', null, null);
INSERT INTO users (id, name, surname, city, country) VALUES (4, 'john', 'abc', null, null);
INSERT INTO users (id, name, surname, city, country) VALUES (5, 'abcd', 'abc', null, null);
INSERT INTO users (id, name, surname, city, country) VALUES (6, 'abcd', 'abc', null, null);
结果,我得到了前四行。存储库仅匹配第一个参数名称,不匹配姓氏。是什么原因?
And as a result I get first four rows. Repository matches only first parameter "name" and doesn't match "surname". What is the cause?
(UserSearchCriteria与用户的字段相同:姓名,姓氏,城市,国家/地区)。
(UserSearchCriteria has the same fields as User: name, surname, city, country).
推荐答案
您正在使用单个if-else,因此每次都只匹配一个条件。
You are using a single if-else, so only one conditions is matched each time.
尝试将其更改为一个独立的if序列:
Try changing it to a sequence of standalone ifs:
@Override
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
final List<Predicate> predicates = new ArrayList<>();
if (criteria.getName() != null) {
final Predicate name = builder.equal(root.<String>get("name"), criteria.getName());
predicates.add(name);
}
if (criteria.getSurname() != null) {
final Predicate surname = builder.equal(root.<String>get("surname"), criteria.getSurname());
predicates.add(surname);
}
if (criteria.getCity() != null) {
final Predicate city = builder.equal(root.<String>get("city"), criteria.getCity());
predicates.add(city);
}
if (criteria.getCountry() != null) {
final Predicate country = builder.equal(root.<String>get("country"), criteria.getCountry());
predicates.add(country);
}
return builder.and(predicates.toArray(new Predicate[predicates.size()]));
}
这篇关于春季规范-谓词的合取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!