如何解读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()));
也是有明显错误。文章来源:https://www.toymoban.com/news/detail-519028.html
Bard、文心一言水平差不多,ChatGPT独领风骚。文章来源地址https://www.toymoban.com/news/detail-519028.html
到了这里,关于Bard、一言、ChatGPT的水平的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!