gpt4 book ai didi

Spring 规范 - 谓词的结合

转载 作者:行者123 更新时间:2023-12-04 03:02:15 27 4
gpt4 key购买 nike

我需要一个函数来过滤参数和构建查询。我有 4 个参数,因此如果我尝试为每个条件实现查询,我将不得不写 16 (2^4)实现 - 这不是一个好主意。

我尝试使用界面改进我的代码 Specification来自 Spring Data JPA,但我无法创建谓词的连接。
Specification的实现界面 :

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);
}

在测试之前,我将以下用户插入到数据库中:
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);

结果我得到了前四行。 Repository 只匹配第一个参数“name”,不匹配“surname”。原因是什么?

(UserSearchCriteria 具有与用户相同的字段:姓名、姓氏、城市、国家/地区)。

最佳答案

您使用的是单个 if-else,因此每次只匹配一个条件。

尝试将其更改为一系列独立的 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()]));
}

关于Spring 规范 - 谓词的结合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48210163/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com