【问题标题】:How to use Q Classes of Querydsl?如何使用 Querydsl 的 Q 类?
【发布时间】:2018-03-30 14:09:52
【问题描述】:

我想将 QueryDSLJPA 一起使用,并且我成功插入了 apt-maven

Pom.xml;

     <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <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>com.querydsl.apt.jpa.JPAAnnotationProcessor </processor>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

同样它也成功生成了领域类的Q类,但是当我import com.buraku.netas.domain.QUser;时,它会抛出错误并且找不到Q类。

这是我运行 mvn install 后的文件夹结构;

如您所见,生成了 Q 类,但我还应该怎么做才能使用它们?

【问题讨论】:

    标签: java jpa querydsl


    【解决方案1】:

    首先,将实现QuerydslPredicateExecutor&lt;User&gt;添加到接口UserRepository

    其次,创建一个返回com.querydsl.core.types.Predicate 的方法并使用BooleanBuilderQ-classes 构建一个查询(JPQL)。

    您可以参考我在 github 上的 sample-jpa 代码。

    jpa-sample-code

    【讨论】:

    • 但是我不能使用 Q-classes,因为那些 Q-classes 会抛出一个错误,说找不到。我做了你说的第一件事并运行 mvn clear install 但它仍然不允许我导入 Q 类
    • 对了,能把pom.xml分享到github上吗?
    • 我没有使用 maven,而是使用了 gradle。但我发现你的问题是你的输出目录有问题。它应该是“src/main/generated_sources/”。或者将你当前的 outputDirectory 添加到类路径中。
    • 看看我的 build.gradle,它用“src/main/java”和“src/main/generated”指向 sourceSets。它可以是一个提示。也许你需要从中找到一个等价的maven表达式。
    猜你喜欢
    • 2014-09-13
    • 2012-12-06
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多