Free Lines Arrow
본문 바로가기
DataBase/JPA

[QueryDSL] QueryDSL 설정2 - Mysql 연동

by skahn1215 2024. 11. 25.
728x90
반응형

QueryDSL 설정2

  • Mysql 을 연동해본다
  • application-test.yaml 로 테스트 코드 설정을 분리한다

 

MySql Connector 추가

implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.27'

 

 

 

 

Test code Yaml 분리

  • gradle 에 아래와 같이 추가 하면 TestCode 에서 에러가 발생할 것이다. 
  • 이유는 테스트 코드에서 접속을 시도하는데 아직은 DB를 연동하지 않아서 발생한다.
  • 연동이 되어도 테스트코드는 별도로 수행하는게 바람직하기 때문에 분리하는 해야한다.

 

application-test.yaml 추가

test 폴더 아래에 application-test.yaml을 추가해준다.

 

application-test.yaml

spring:
  # This configuration will be active only in the "test" profile
  datasource:
    url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE  # In-memory H2 database
    driver-class-name: org.h2.Driver
    username: sa
    password:
  jpa:
    hibernate:
      ddl-auto: create-drop  # Automatically create/drop schema for each test run
    show-sql: true
    properties:
      hibernate:
        format_sql: true
    database-platform: org.hibernate.dialect.H2Dialect

 

 

Test code Profile 변경

  • 아래와 같이 test 코드를 돌릴때 application-test.yaml 을 바라보도록 한다.
@SpringBootTest
@ActiveProfiles("test")
class QuerydslApplicationTests {

	@Test
	void contextLoads() {
	}

}

 

 

Clean -> Build

Clean

오후 8:00:13: Executing 'clean'…

> Task :clean

BUILD SUCCESSFUL in 700ms
1 actionable task: 1 executed
오후 8:00:13: Execution finished 'clean'.

Build

오후 8:00:43: Executing 'build'…

> Task :compileJava
> Task :processResources
> Task :classes
> Task :resolveMainClassName
> Task :bootJar
> Task :jar
> Task :assemble
> Task :compileTestJava
> Task :processTestResources
> Task :testClasses
OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
2024-11-25T20:00:47.950+09:00  INFO 47094 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2024-11-25T20:00:47.951+09:00  INFO 47094 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2024-11-25T20:00:47.952+09:00  INFO 47094 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
> Task :test
> Task :check
> Task :build

BUILD SUCCESSFUL in 4s
8 actionable tasks: 8 executed
오후 8:00:48: Execution finished 'build'.

 

 

 

build.gradle 전체 파일

plugins {
	id 'java'
	id 'org.springframework.boot' version '3.4.0'
	id 'io.spring.dependency-management' version '1.1.6'
}

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

java {
	toolchain {
		languageVersion = JavaLanguageVersion.of(17)
	}
}

configurations {
	compileOnly {
		extendsFrom annotationProcessor
	}
}

repositories {
	mavenCentral()
}


dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.springframework.boot:spring-boot-starter-webflux'
	compileOnly 'org.projectlombok:lombok'
	annotationProcessor 'org.projectlombok:lombok'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	testImplementation 'io.projectreactor:reactor-test'
	testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

	// queryDSL
	implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
	annotationProcessor "com.querydsl:querydsl-apt:5.0.0:jakarta"
	annotationProcessor "jakarta.annotation:jakarta.annotation-api"
	annotationProcessor "jakarta.persistence:jakarta.persistence-api:3.1.0"

	// 테스트 코드를 위한 DB 설정
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	testRuntimeOnly 'com.h2database:h2'

	implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.27'
}

def querydslGeneratedDir = project.hasProperty('querydsl.generated.dir') ?
		project.property('querydsl.generated.dir') : "$buildDir/generated/querydsl"

// 소스셋 설정: Java 소스와 QClass 파일이 생성되는 디렉토리 경로 포함
sourceSets {
	main {
		java {
			srcDirs = ['src/main/java', querydslGeneratedDir]
		}
	}
}


// QClass 생성 후, 해당 경로를 설정하는 작업
compileJava {
	options.annotationProcessorPath = configurations.annotationProcessor
	options.compilerArgs = ['-processor', 'com.querydsl.apt.jpa.JPAAnnotationProcessor']
}


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

 

 

 

여기까지 왔다면 QueryDsl 설정이 끝난것이다.

전체코드

https://github.com/rnrl1215/querydsl/tree/main/src

 

querydsl/src at main · rnrl1215/querydsl

jpa 와 querydsl. Contribute to rnrl1215/querydsl development by creating an account on GitHub.

github.com

 

728x90
반응형

'DataBase > JPA' 카테고리의 다른 글

[QueryDSL] QueryDSL 설정 3 - DataSource  (0) 2024.11.26
[QueryDSL] QueryDSL 설정 1 - QueryDsl 설정 적용  (0) 2024.11.24
[JPA] AttributeConverter  (0) 2022.04.02
[JPA] JPA의 설정 옵션들  (0) 2022.01.26
[JPA] JPQL Named 쿼리  (0) 2021.09.28

댓글