在 Query DSL 中没有找到查询类。但是Q Class已经存在并且Intellij已经设置

lim*_*ain 5 intellij-idea gradle querydsl spring-boot

我正在尝试在我的项目中使用 QDataTableRepository 。这让我必须导入querydsl。

构建.gradle

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'idea'

repositories {
    mavenCentral()
}


dependencies {
    compile("org.springframework.boot:spring-boot-devtools")
    compile('org.springframework.boot:spring-boot-starter')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.postgresql:postgresql:9.4.1212')
    compile('org.projectlombok:lombok:1.16.10')
    compile group: 'com.github.darrachequesne', name: 'spring-data-jpa-datatables', version: '3.1'
    compile group: 'com.querydsl', name: 'querydsl-jpa', version: '4.1.4'
    compile group: 'com.querydsl', name: 'querydsl-apt', version: '4.1.4'

    testCompile('org.springframework.boot:spring-boot-starter-test')

}

configurations {
    querydslapt
}

sourceSets {
    generated
}
sourceSets.generated.java.srcDirs = ['generated/']

task generateQueryDSL(type: JavaCompile, group: 'build', description: 'Generates the QueryDSL query types') {
    source = sourceSets.main.java
    classpath = configurations.compile + configurations.querydslapt
    options.compilerArgs = [
            "-proc:only",
            "-processor", "com.mysema.query.apt.jpa.JPAAnnotationProcessor"
    ]
    destinationDir = sourceSets.generated.java.srcDirs.iterator().next()
}

compileGeneratedJava {
    dependsOn generateQueryDSL
    classpath += sourceSets.main.runtimeClasspath
}
Run Code Online (Sandbox Code Playgroud)

我还在Intellij 中设置了注释处理器。

注释处理器设置图片

我构建了我的项目并构建成功。里面生成了QClass。
这里

结构

/**
 * QClient is a Querydsl query type for Client
 */
@Generated("com.mysema.query.codegen.EntitySerializer")
public class QClient extends EntityPathBase<Client> {
Run Code Online (Sandbox Code Playgroud)

但是当我运行项目时出现错误。

Caused by: java.lang.IllegalArgumentException: Did not find a query class com.project.module.client.QClient for domain class om.project.module.client.Client!
Run Code Online (Sandbox Code Playgroud)

我是否缺少配置?

Pet*_*ski 0

对于所有仍然遇到此问题的人 - 就像我时不时做的那样:

默认情况下,在我的例子中,Q 类是在target/generated-sources/annotations文件夹中生成的。该文件夹还会自动标记为源文件夹(文件 -> 项目结构 -> 模块 -> 源):

在此输入图像描述

对我有什么帮助

删除此源文件夹并选择以下内容作为源文件夹: target/generated-sources/annotations/com

在此输入图像描述

单击“应用”->“确定”并运行该项目。

有关基于 Gradle 的解决方案,请参阅此答案。