我正在使用 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);
}
}
我厌倦了搜索,我没有找到任何做过类似事情的人。有人对如何做到这一点有任何建议吗?
蓝山帝景
凤凰求蛊
相关分类