多列“OR"在 Spring Data Rest 中使用 QueryDsl [英] Multi-column "OR" in Spring Data Rest with QueryDsl
问题描述
是否可以使用 Spring (Boot) Data Rest 和 QueryDsl 执行跨多列的或"搜索?我已经自定义了我的绑定,以便在多次出现相同的搜索路径时执行或"搜索(MultiValueBinding).myentity?name=foo&name=bar
将返回 name 属性包含 foo
或 bar
的所有实体.
Is it possible with Spring (Boot) Data Rest and QueryDsl to perform an "or" search spanning multiple columns? I have already customized my bindings so that an "or" search will be executed when the same search path is present multiple times (MultiValueBinding). myentity?name=foo&name=bar
will return all entities where the name attribute either contains foo
or bar
.
@Entity
MyEntity {
@Id
Long id;
String name;
String email;
}
@RepositoryRestResource(path = "myentity")
public interface MyEntityRepository extends CrudRepository<MyEntity, Long>, ,
QuerydslPredicateExecutor<MyEntity>, QuerydslBinderCustomizer<QMyEntity> {
@Override
default void customize(QuerydslBindings bindings, QMyEntity root) {
bindings.bind(root.id).first(NumberExpression::eq);
bindings.bind(String.class).all((StringPath path, Collection<? extends String> values) -> {
BooleanBuilder predicate = new BooleanBuilder();
values.forEach(value -> predicate.or(path.containsIgnoreCase(value)));
return Optional.of(predicate);
});
}
}
如果不实现自定义控制器,是否可以通过or"连接多个路径中的搜索,以便 myentity?name=foo&email=bar
将返回 name 属性包含foo"的结果" 或电子邮件属性包含bar".或者定义一个自定义路径,然后用于在多列中进行搜索?例如myentity?nameOrEmail=foo
.
Without implementing a custom controller, is it possible to connect searches in multiple paths via "or" so that myentity?name=foo&email=bar
will return results where the name attribute contains "foo" or the the email attribute contains "bar". Alternatively defining a custom path which is then used for searches in multiple columns? e.g. myentity?nameOrEmail=foo
.
推荐答案
你可以只使用这个库:https://github.com/turkraft/spring-filter
它将让您运行搜索查询,例如:
It will let you run search queries such as:
/search?filter= 平均(评分)> 4.5 和brand.name 在('奥迪'、'路虎')和(年份> 2018或公里< 50000) 和颜色 : '白色' 和 事故 为空
/search?filter= average(ratings) > 4.5 and brand.name in ('audi', 'land rover') and (year > 2018 or km < 50000) and color : 'white' and accidents is empty
您也可以查看https://github.com/jirutka/rsql-parser但它有点过时了
You may also check https://github.com/jirutka/rsql-parser but it is a bit outdated
这篇关于多列“OR"在 Spring Data Rest 中使用 QueryDsl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!