如何解读Specification.where
如何解读Spring boot jpa中Specification的如下方法:
static <T> Specification<T> where(@Nullable Specification<T> spec) {
return spec == null ? (root, query, builder) -> null : spec;
}
有文字解释,这里只摘录代码部分。
Bard
Specification<User> spec1 = new Specification<User>() {
@Override
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
return builder.equal(root.get("name"), "John Doe");
}
};
Specification<User> spec2 = new Specification<User>() {
@Override
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
return builder.greaterThan(root.get("age"), 21);
}
};
Specification<User> spec = Specification.where(spec1).where(spec2);
List<User> users = repository.findAll(spec);
这句Specification<User> spec = Specification.where(spec1).where(spec2);
明显错误。
ChatGPT
Specification<Book> spec1 = (root, query, builder) -> builder.lessThanOrEqualTo(root.get(Book_.PRICE), 100);
Specification<Book> spec2 = (root, query, builder) -> builder.like(root.get(Book_.ISBN), "%64%");
Specification<Book> spec = spec1.and(spec2);
List<Book> books = bookRepository.findAll(spec);
books.forEach(b -> log.debug("{}{-}", b.getIsbn(), b.getPrice()));
文心一言
List<Person> people = new ArrayList<>();
people.add(new Person("Alice", 25));
people.add(new Person("Bob", 20));
people.add(new Person("Charlie", 30));
List<Person> filteredPeople = new ArrayList<>();
Specification<Person> specification = Specification.where(builder -> builder.ge(Person::getAge, 18));
filteredPeople.addAll(people.stream().filter(specification).collect(Collectors.toList()));
也是有明显错误。
Bard、文心一言水平差不多,ChatGPT独领风骚。
文章出处登录后可见!
已经登录?立即刷新