简体   繁体   English

QueryDSL 谓词绑定多条路径之间的“或”

[英]QueryDSL predicate bindings "or" between multiple path

I have a Spring boot Rest API witch use Querydsl predicate for filtering, I use the QuerydslBinderCustomizer on my MongoRepository to bind properties path to customize the query.我有一个 Spring Boot Rest API 女巫使用Querydsl谓词进行过滤,我使用MongoRepository上的QuerydslBinderCustomizer来绑定属性路径来自定义查询。

The default behavior when multiple search values are set for the same path is an OR operator between these values.为同一路径设置多个搜索值时的默认行为是这些值之间的 OR 运算符。

But when multiple path are used and AND operator is used.但是当使用多个路径并使用 AND 运算符时。

What I want to do is to put an OR operator between multiple chosen path.我想要做的是在多个选择的路径之间放置一个 OR 运算符。

I found this quite clear documentation but didin't find an answer in it : https://gt-tech.bitbucket.io/spring-data-querydsl-value-operators/README.html我找到了这个非常清楚的文档,但没有在其中找到答案: https : //gt-tech.bitbucket.io/spring-data-querydsl-value-operators/README.html

Today I have :今天我有:

CONTROLLER控制器

public Page<Document> find(
            @QuerydslPredicate(root = Document.class) Predicate predicate) {
        return repository.findAll(predicate);
    }

REPOSITORY存储库

public interface Repository extends
        MongoRepository<Document, String>,
        QueryDslPredicateExecutor<EventRuleDocument>,
        QuerydslBinderCustomizer<QEventRuleDocument> {

    @Override
    default void customize(QuerydslBindings bindings, QDocument doc) {
        bindings.bind(doc.prop1).first(StringExpression::containsIgnoreCase);
        bindings.bind(doc.prop2).first(StringExpression::containsIgnoreCase);
        bindings.bind(doc.prop3).first(StringExpression::containsIgnoreCase);
    }
}

Witch gives me the predicate : prop1 == v1 AND prop2 == v2 AND prop3 == v3女巫给了我谓词: prop1 == v1 AND prop2 == v2 AND prop3 == v3

What I would like to achieve is : prop1 == v1 OR prop2 == v2 OR prop3 == v3我想实现的是: prop1 == v1 OR prop2 == v2 OR prop3 == v3

Is this possible using QuerydslBinderCustomizer ?这可以使用QuerydslBinderCustomizer吗?

Or do I have to create a custom controller and/or repository method ?还是我必须创建自定义控制器和/或存储库方法?

@Override
default void customize(QuerydslBindings bindings, QDocument root) {

StringPath[] multiPropertySearchPaths = new StringPath[] {root.prop1, root.prop2, root.prop3};

/**
 * Binds prop1, prop2 and prop3 in OR clause
 * This binding will activate when one of the given properties are searched in query params
 */
bindings.bind(multiPropertySearchPaths).all(new MultiValueBinding<StringPath, String>() {
    @Override
    public Predicate bind(StringPath path, Collection<? extends String> values) {
        BooleanBuilder predicate = new BooleanBuilder();
        // Bind paths present in array multiPropertySearchPaths with incoming values
        for (StringPath propertyPath : multiPropertySearchPaths) {
            values.forEach(value -> predicate.or(propertyPath.containsIgnoreCase(value)));
        }
        return predicate;
    }
});
}

It will generate sql like:它将生成如下 sql:

select * from document
where
    lower(document.prop1) like ?1 
    or lower(document.prop2) like ?1 
    or lower(document.prop3) like ?1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM