article thumbnail image
Published 2021. 10. 6. 17:42

QueryDsl 적용

 

build.gradle

//(1)
buildscript {
	dependencies {
		classpath("gradle.plugin.com.ewerk.gradle.plugins:querydsl-plugin:1.0.10")
	}
}

plugins {
	id 'org.springframework.boot' version '2.7.2'
	id 'io.spring.dependency-management' version '1.0.12.RELEASE'
	id 'java'
}

//(2)
apply plugin: "com.ewerk.gradle.plugins.querydsl"

group = 'jpa'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
	compileOnly {
		extendsFrom annotationProcessor
	}
}

repositories {
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'
	implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.8.0'

	//(3)
	implementation 'com.querydsl:querydsl-jpa'
	implementation 'com.querydsl:querydsl-apt'

	compileOnly 'org.projectlombok:lombok'
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	runtimeOnly 'com.h2database:h2'
	annotationProcessor 'org.projectlombok:lombok'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

//(4)
def querydslDir = 'src/main/generated'

//(5)
querydsl {
	library = "com.querydsl:querydsl-apt"
	jpa = true
	querydslSourcesDir = querydslDir
}

//(6)
sourceSets{
	main{
		java{
			srcDirs = ['src/main/java', querydslDir]
		}
	}
}

//(7)
compileQuerydsl{
	options.annotationProcessorPath = configurations.querydsl
}

//(8)
configurations {
	querydsl.extendsFrom compileClasspath
}

tasks.named('test') {
	useJUnitPlatform()
}

 

QuerydslConfiguration.java 파일 생성

package jpa.board.configuration;

import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * packageName    : jpa.board.configuration
 * fileName       : QuerydslConfiguration
 * author         : 김재성
 * date           : 2022-08-02
 * description    :
 * ===========================================================
 * DATE              AUTHOR             NOTE
 * -----------------------------------------------------------
 * 2022-08-02        김재성       최초 생성
 */
@Configuration
public class QuerydslConfiguration {
    @PersistenceContext
    private EntityManager entityManager;

    @Bean
    public JPAQueryFactory jpaQueryFactory() {
        return new JPAQueryFactory(entityManager);
    }
}

 

gradle task에서 compileQuerydsl 더블 클릭

 

프로젝트 - build - generated - qurydsl 폴더에 Q클래스 파일이 생성됨

복사했습니다!