春季启动在这里。我试图绕开我的头JpaRepositories
,Specifications
当在实现复杂查询的上下文中使用时,我正在努力在几个项目上看到“森林穿过树木”。
a 的典型示例Specification
如下:
public class PersonSpecification implements Specification<Person> {
private Person filter;
public PersonSpecification(Person filter) {
super();
this.filter = filter;
}
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> cq,
CriteriaBuilder cb) {
Predicate p = cb.disjunction();
if (filter.getName() != null) {
p.getExpressions()
.add(cb.equal(root.get("name"), filter.getName()));
}
if (filter.getSurname() != null && filter.getAge() != null) {
p.getExpressions().add(
cb.and(cb.equal(root.get("surname"), filter.getSurname()),
cb.equal(root.get("age"), filter.getAge())));
}
return p;
}
}
在这个toPredicate(...)
方法中,Root<Person>
andCriteriaQuery
代表什么?最重要的是,听起来您需要为要应用的每种Specification
类型的过滤器创建一个impl ,因为每个规范都被翻译成一个且只有一个谓词......所以例如,如果我想找到所有姓氏的人“Smeeb”,年龄超过 25 岁,听起来我需要写 a和. 有人可以为我确认或澄清这一点吗?!LastnameMatchingSpecification<Person>
AgeGreaterThanSpecification<Person>