我正在使用Spring Boot和QueryDSL来执行REST请求,但是我在执行搜索时遇到问题,该搜索在两个字段中执行具有相同值的查询(以澄清,如果值在字段中或另一个字段中)。像这样的东西:
select * from news where title like '%word%' or content like '%word%';
我想做这样的请求(严格来说,可能会有所不同):
http://localhost?title=word&content=word
我在我的RestController中有这个:
@GetMapping
public PagedResources<Resource> get(@QuerydslPredicate(root = News.class) Predicate predicate,
@SortDefaults({
@SortDefault(sort = "type", direction = ASC),
@SortDefault(sort = "dateCreated", direction = DESC)
}) Pageable pageable) {
Page<News> pages = newsService.getAllWithPagination(predicate, pageable);
return pagedResourcesAssembler.toResource(pages, newsResourceAssembler);
}
这是我的存储库:
public interface NewsRepository extends JpaRepository<News, Long>, QuerydslPredicateExecutor<News>, QuerydslBinderCustomizer<QNews> {
@Override
default void customize(QuerydslBindings bindings, QNews news) {
bindings.bind(String.class)
.first((SingleValueBinding<StringPath, String>) StringExpression::containsIgnoreCase);
}
}
我厌倦了搜索,我没有找到任何类似的东西。有人对如何做到这一点有任何建议吗?
QNews qnews = QNews.news; final BooleanExpression booleanExpression = qnews.title.like(“%word%”)。或(qnews .content.like(“%word%”));
就在这之前检查queryparam中的空值
随着@kumar的回答和更多的研究,我终于实现了我想要的:
@Override
default void customize(QuerydslBindings bindings, QNews news) {
bindings.bind(news.title)
.first((path, value) -> path.likeIgnoreCase("%" + value + "%").or(news.content.likeIgnoreCase("%" + value + "%")));
}