You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot Gradle(MongoDB):IntelliJ保存文件自动生成Querydsl查询类?

Absolutely! You can get Querydsl query types to auto-generate when you save your entity files—just like how Lombok works—with a few tweaks to your Gradle setup and IDE configuration. Let’s break this down step by step for your Spring Boot + Spring Data MongoDB + Gradle project:

Step 1: Update Your Gradle Build Configuration

First, make sure your build.gradle (Groovy DSL) includes the correct dependencies and annotation processing setup. Here’s a working example tailored to your stack:

plugins {
    id 'org.springframework.boot' version '3.2.0' // Use your project's Spring Boot version
    id 'io.spring.dependency-management' version '1.1.4'
    id 'java'
}

group = 'com.yourcompany'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '17' // Match your project's Java version
}

repositories {
    mavenCentral()
}

dependencies {
    // Spring Data MongoDB
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
    
    // Querydsl for MongoDB
    implementation 'com.querydsl:querydsl-mongodb'
    // Annotation processor for Querydsl, configured to work with Lombok
    annotationProcessor 'com.querydsl:querydsl-apt:5.0.0:jpa,lombok' // Match Querydsl version
    
    // Lombok (if you're using it)
    implementation 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
}

// Make sure Gradle and IDE recognize the generated sources directory
sourceSets {
    main {
        java {
            srcDirs += 'build/generated/sources/annotationProcessor/java/main'
        }
    }
}

Key notes here:

  • Ensure the Querydsl version matches across querydsl-mongodb and querydsl-apt.
  • The :jpa,lombok suffix on the querydsl-apt dependency tells the processor to handle both Spring Data annotations and Lombok-generated fields.
  • We explicitly add the generated sources directory to the main source set so your IDE can pick up the Q* classes.

Step 2: Configure IntelliJ IDEA for Auto-Generation

Most Spring Boot developers use IntelliJ, so here’s how to set it up to trigger annotation processing on save:

  1. Open Settings → Build, Execution, Deployment → Compiler → Annotation Processors
  2. Check the box for Enable annotation processing
  3. Under Generated sources directory, ensure the path matches what’s in your Gradle config: build/generated/sources/annotationProcessor/java/main
  4. Check Store generated sources relative to module content root
  5. (Optional) Under the Processors tab, manually add com.querydsl.apt.spring.SpringDataAnnotationProcessor if the IDE doesn’t detect it automatically—this is the processor that handles Spring Data MongoDB’s @Document annotations.
  6. Go to Settings → Build, Execution, Deployment → Build Tools → Gradle
    • Set Build and run using to IntelliJ IDEA
    • Set Run tests using to IntelliJ IDEA
      This tells the IDE to handle annotation processing itself instead of relying on manual Gradle builds.

Step 3: Verify Your Entity Class

Make sure your domain entity is annotated correctly so Querydsl can generate the query type:

package com.yourcompany.yourproject.entity;

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "users")
@Data // Lombok's @Data to generate getters/setters
public class User {
    @Id
    private String id;
    private String username;
    private String email;
    private int age;
}

After saving this file, you should see a QUser class appear in the build/generated/sources/annotationProcessor/java/main/com/yourcompany/yourproject/entity directory automatically.

Troubleshooting Common Issues

  • No Q* classes generated? Double-check that annotation processing is enabled in IDEA, and that the generated sources directory is marked as a Generated Sources Root (right-click the directory → Mark Directory as → Generated Sources Root).
  • Version mismatch errors? Ensure all Querydsl dependencies use the same version. You can use Spring Dependency Management to enforce this if needed.
  • Lombok fields not recognized? Make sure the querydsl-apt dependency includes the :lombok suffix, and that Lombok’s annotation processor is configured correctly.

内容的提问来源于stack exchange,提问作者henriquels

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:25:37