Querydsl的Q类如何使用?

How to use Q Classes of Querydsl?

我想将 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 类 已生成,但我还应该做什么才能使用它们?

首先,将实现 QuerydslPredicateExecutor<User> 添加到接口 UserRepository

其次,创建一个 returns com.querydsl.core.types.Predicate 的方法并使用 BooleanBuilderQ-classes.

构建查询(JPQL)

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

jpa-sample-code