将任何文本匹配到搜索框中
Match for any text into search box
我使用此搜索规范尝试按标题搜索文本。如果我输入准确的搜索字符串,它工作正常:
public Page<ClassCategoriesFullDTO> findClasses(ClassCategoriesSearchParams params, Pageable pageable) {
Specification<Product> spec = (root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();
if (params.getTitle() != null) {
predicates.add(cb.equal(root.get("title"), params.getTitle()));
}
if (params.getType() != null) {
predicates.add(cb.equal(root.get("type"), params.getType()));
}
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
};
return classCategoriesService.findAll(spec, pageable).map(classCategoriesMapper::toFullDTO);
}
我如何修改代码以使用我在数据库中键入的每个文本搜索数据库 table?
您可以使用条件生成器 (cb) 之类的方法来搜索“an”以获取“surname”
static Specification<ClassCategoriesFullDTO> titleContains(String title) {
return (root, cq, cb) -> cb.like(root.get("title"), "%" + title + "%");
}
如果您想忽略大小写,请使用:
static Specification<ClassCategoriesFullDTO> titleContains(String title) {
return (root, cq, cb) -> cb.like(cb.lower(root.get("title")), "%" + title.toLowerCase() + "%");
}
我使用此搜索规范尝试按标题搜索文本。如果我输入准确的搜索字符串,它工作正常:
public Page<ClassCategoriesFullDTO> findClasses(ClassCategoriesSearchParams params, Pageable pageable) {
Specification<Product> spec = (root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();
if (params.getTitle() != null) {
predicates.add(cb.equal(root.get("title"), params.getTitle()));
}
if (params.getType() != null) {
predicates.add(cb.equal(root.get("type"), params.getType()));
}
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
};
return classCategoriesService.findAll(spec, pageable).map(classCategoriesMapper::toFullDTO);
}
我如何修改代码以使用我在数据库中键入的每个文本搜索数据库 table?
您可以使用条件生成器 (cb) 之类的方法来搜索“an”以获取“surname”
static Specification<ClassCategoriesFullDTO> titleContains(String title) {
return (root, cq, cb) -> cb.like(root.get("title"), "%" + title + "%");
}
如果您想忽略大小写,请使用:
static Specification<ClassCategoriesFullDTO> titleContains(String title) {
return (root, cq, cb) -> cb.like(cb.lower(root.get("title")), "%" + title.toLowerCase() + "%");
}