Spring Data MongoDB Repository - JPA 规范,如 [英] Spring Data MongoDB Repository - JPA Specifications like

查看:25
本文介绍了Spring Data MongoDB Repository - JPA 规范,如的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring Data MongoDB 存储库是否有类似 JPA 规范 的东西?

Is there something like JPA Specifications for Spring Data MongoDB Repositories?

如果没有,我如何使用存储库进行动态查询?

If not, how can I make dynamic queries with repositories?

一个经典的场景可能是一个带有用户将填写的可选字段的搜索表单.

A classic scenario could be a search form with optional fields that the user will fill.

推荐答案

我找到了自己的方法.

这个技巧可以使用 QueryDSL 来完成,方法如下:

The trick can be done using QueryDSL, in the following way:

首先,添加 QueryDSL 依赖项:

First, add the QueryDSL dependencies:

<dependency>
    <groupId>com.mysema.querydsl</groupId>
    <artifactId>querydsl-mongodb</artifactId>
    <version>${querydsl-mongo.version}</version>
</dependency>

<dependency>
    <groupId>com.mysema.querydsl</groupId>
    <artifactId>querydsl-apt</artifactId>
    <version>${querydsl-mongo.version}</version>
</dependency>

然后,配置插件以创建元模型类(它们的名称将与 documents 相同,前缀为字母 Q:例如.QUser):

Then, configure the plugin in order to create Metamodels classes (their names will be the same of the documents with the letter Q as prefix: eg. QUser):

    <build>
        <plugins>
            ....
            <plugin>
                <groupId>com.mysema.maven</groupId>
                <artifactId>apt-maven-plugin</artifactId>
                <version>1.1.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/generated-sources/java</outputDirectory>
                            <processor>org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor</processor>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

注意处理器类:它不是 QueryDSL 默认的一个 com.mysema.query.apt.morphia.MorphiaAnnotationProcessor,而是 Spring Data MongoDB 的一个 org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor:

Note the processor class: it's not the QueryDSL default one com.mysema.query.apt.morphia.MorphiaAnnotationProcessor, but the Spring Data MongoDB one org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor:

Spring Data Mongo 提供了一个自定义的 APT 处理器来生成Metamodels 而不是 QueryDSL 中提供的,它将扫描Spring 特定的 @Document 而不是 Morphia 特定的注释.

Spring Data Mongo provides a custom APT processor to generate the Metamodels instead of the one provided in QueryDSL, it will scan the Spring specific @Document instead of the Morphia specific annotations.

现在我们可以让我们的存储库接口扩展QueryDslPredicateExecutor:

Now we can make our repository interface extending QueryDslPredicateExecutor<T>:

public interface UserRepository extends MongoRepository<User, String>, QueryDslPredicateExecutor<User>,
        QuerydslBinderCustomizer<QUser> {
}

我们现在可以在查询存储库时定义谓词:

We can now define Predicates when querying the repository:

QUser user = QUser.user;            
BooleanExpression predicate = user.name.containsIgnoreCase("John");         
userRepository.findAll(predicate);

QuerydslBinderCustomizer 帮助您定义文档属性的绑定(参见 Spring 文档 以获得更多帮助).

QuerydslBinderCustomizer helps you to define the binding of the Document's properties (see Spring documentation for further help).

这篇关于Spring Data MongoDB Repository - JPA 规范,如的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆