带有Query Dsl自定义绑定的Spring和/或操作不起作用 [英] Spring with Query Dsl custom binding and or operation doesn't work
问题描述
我有以下需求查询:
"/文章?类别 =厨房和类别 =运动"
"/article?category=kitchen&category=sports"
此查询在没有自定义绑定的情况下工作.它将给我所有关于厨房和体育的文章作为回应.它正在以某种方式进行或"操作.
This Query is working without custom bindings. It will give me all Kitchen and sports articles as Response. It is doing an OR-Operation somehow.
但是我需要自定义绑定,因为我需要了解情况.现在,我正在使用此自定义绑定:
But i need to customize the binding because i need to ingore the case. Now i am using this customize binding:
@Repository
public interface ArticleRepository extends JpaRepository<Article, Long>,
QueryDslPredicateExecutor<QArticle>, QuerydslBinderCustomizer<QArticle> {
@Override
default public void customize(QuerydslBindings bindings, QArticle article) {
bindings.bind(String.class).first((StringPath path, String value) -> path.containsIgnoreCase(value));
}
}
这只会过滤属性的第一个可用值.因此,在这种情况下,它仅将类别厨房"中的文章作为响应".类别(运动)的第二个值将被忽略.
This only filters the first available value of a attribut. So in this case it only gives articles in category kitchen as Response. The second value of the category (sports) is ignored.
现在我的问题是:我如何才能忽略两者类别的文章,而忽略大小写?我需要在自定义绑定中进行哪些更改以实现该目标?
And now my Question: How can i get the articles of both catergories ignoring the case sensetive? What i need to change in my customize binding to achieve that?
非常感谢您
推荐答案
article.category
属性的正确绑定应如下所示:
The correct bindings for your article.category
property should be like this:
bindings.bind(article.category).all((path, value) -> {
BooleanBuilder predicate = new BooleanBuilder();
value.forEach(o -> predicate.or(path.equalsIgnoreCase(o)));
return Optional.of(predicate);
});
如果不需要任何参数操作(例如忽略大小写),则可以简化为:
If you don't need any manipulations of the parameters (like ignoring case) you can simplify it to that:
bindings.bind(article.category).all((path, value) -> Optional.of(path.in(value)));
这篇关于带有Query Dsl自定义绑定的Spring和/或操作不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!