将任何文本匹配到搜索框中

huwehgph  于 2021-07-16  发布在  Java
关注(0)|答案(1)|浏览(290)

我使用这个搜索规范来尝试按标题搜索文本。如果我输入准确的搜索字符串就可以了:

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

如何修改代码,以便在db表中键入的每个文本中搜索db?

pu3pd22g

pu3pd22g1#

您可以像使用criteria builder(cb)一样搜索“an”以获得“姓氏”

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() + "%");
}

相关问题