From 544cec0e813f5b622ac9c4e818ff23ee34f35d1c Mon Sep 17 00:00:00 2001 From: sohot8653 Date: Mon, 11 Aug 2025 09:20:35 +0900 Subject: [PATCH] =?UTF-8?q?[QueryDSL=20=EC=84=B8=ED=8C=85=20=EB=B0=8F=20?= =?UTF-8?q?=EC=A0=84=EC=B2=B4=20=EA=B5=AC=EC=A1=B0=20=EC=84=B8=ED=8C=85]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 30 ++++++++++++++++++- .../controller/TestController.java | 0 .../bio_backend/{ => domain}/entity/Test.java | 0 .../repository/TestRepository.java | 0 .../global/aop/RepositoryLoggingAspect.java | 30 +++++++++++++++++++ .../bio_backend/global/config/AppConfig.java | 14 +++++++++ src/main/resources/application.properties | 29 +++++++++++++----- 7 files changed, 94 insertions(+), 9 deletions(-) rename src/main/java/com/bio/bio_backend/{ => domain}/controller/TestController.java (100%) rename src/main/java/com/bio/bio_backend/{ => domain}/entity/Test.java (100%) rename src/main/java/com/bio/bio_backend/{ => domain}/repository/TestRepository.java (100%) create mode 100644 src/main/java/com/bio/bio_backend/global/aop/RepositoryLoggingAspect.java create mode 100644 src/main/java/com/bio/bio_backend/global/config/AppConfig.java diff --git a/build.gradle b/build.gradle index 97b6a1b..2f5e3a6 100644 --- a/build.gradle +++ b/build.gradle @@ -23,6 +23,12 @@ repositories { mavenCentral() } +buildscript { + ext { + queryDslVersion = "5.0.0" + } +} + dependencies { // 개발용 의존성 추가 developmentOnly 'org.springframework.boot:spring-boot-devtools' @@ -34,9 +40,31 @@ dependencies { annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' - + + // querydsl + implementation "com.querydsl:querydsl-jpa:${queryDslVersion}:jakarta" + annotationProcessor "com.querydsl:querydsl-apt:${queryDslVersion}:jakarta" + annotationProcessor "jakarta.annotation:jakarta.annotation-api" + annotationProcessor "jakarta.persistence:jakarta.persistence-api" + // p6spy + implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.9.0' } tasks.named('test') { useJUnitPlatform() } + +// querydsl +def querydslDir = "$buildDir/generated/querydsl" + +sourceSets { + main.java.srcDirs += [ querydslDir ] +} + +tasks.withType(JavaCompile) { + options.annotationProcessorGeneratedSourcesDirectory = file(querydslDir) +} + +clean.doLast { + file(querydslDir).deleteDir() +} diff --git a/src/main/java/com/bio/bio_backend/controller/TestController.java b/src/main/java/com/bio/bio_backend/domain/controller/TestController.java similarity index 100% rename from src/main/java/com/bio/bio_backend/controller/TestController.java rename to src/main/java/com/bio/bio_backend/domain/controller/TestController.java diff --git a/src/main/java/com/bio/bio_backend/entity/Test.java b/src/main/java/com/bio/bio_backend/domain/entity/Test.java similarity index 100% rename from src/main/java/com/bio/bio_backend/entity/Test.java rename to src/main/java/com/bio/bio_backend/domain/entity/Test.java diff --git a/src/main/java/com/bio/bio_backend/repository/TestRepository.java b/src/main/java/com/bio/bio_backend/domain/repository/TestRepository.java similarity index 100% rename from src/main/java/com/bio/bio_backend/repository/TestRepository.java rename to src/main/java/com/bio/bio_backend/domain/repository/TestRepository.java diff --git a/src/main/java/com/bio/bio_backend/global/aop/RepositoryLoggingAspect.java b/src/main/java/com/bio/bio_backend/global/aop/RepositoryLoggingAspect.java new file mode 100644 index 0000000..6bc5d3b --- /dev/null +++ b/src/main/java/com/bio/bio_backend/global/aop/RepositoryLoggingAspect.java @@ -0,0 +1,30 @@ +package com.qsl.qsl_tutorial.base; + +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.springframework.stereotype.Component; + +@Aspect +@Component +@Slf4j +public class RepositoryLoggingAspect { + @Around("execution(* org.springframework.data.repository.Repository+.*(..))") + public Object logQueryCall(ProceedingJoinPoint pjp) throws Throwable { + long t0 = System.currentTimeMillis(); + String type = pjp.getSignature().getDeclaringTypeName(); + String method = pjp.getSignature().getName(); + Object[] args = pjp.getArgs(); + + log.info("[QUERY CALL] {}.{}(args={})", type, method, java.util.Arrays.toString(args)); + try { + Object result = pjp.proceed(); + log.info("[QUERY DONE] {}.{}() in {}ms", type, method, (System.currentTimeMillis() - t0)); + return result; + } catch (Throwable ex) { + log.warn("[QUERY FAIL] {}.{}() -> {}", type, method, ex.toString()); + throw ex; + } + } +} \ No newline at end of file diff --git a/src/main/java/com/bio/bio_backend/global/config/AppConfig.java b/src/main/java/com/bio/bio_backend/global/config/AppConfig.java new file mode 100644 index 0000000..49ded59 --- /dev/null +++ b/src/main/java/com/bio/bio_backend/global/config/AppConfig.java @@ -0,0 +1,14 @@ +package com.qsl.qsl_tutorial.base; + +import com.querydsl.jpa.impl.JPAQueryFactory; +import jakarta.persistence.EntityManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class AppConfig { + @Bean + public JPAQueryFactory jpaQueryFactory(EntityManager entityManager) { + return new JPAQueryFactory(entityManager); + } +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index bb6ec07..dbca664 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -9,9 +9,9 @@ spring.devtools.restart.additional-paths=src/main/java # 데이터베이스 연결 정보 # URL 형식: jdbc:postgresql://[호스트명]:[포트번호]/[데이터베이스명] -spring.datasource.url=jdbc:postgresql://stam.kr:15432/mms -spring.datasource.username=mms_user -spring.datasource.password=tam1201 +spring.datasource.url=jdbc:postgresql://stam.kr:15432/imas +spring.datasource.username=imas_user +spring.datasource.password=stam1201 spring.datasource.driver-class-name=org.postgresql.Driver # JPA/Hibernate 설정 @@ -21,13 +21,26 @@ spring.datasource.driver-class-name=org.postgresql.Driver # - update: 엔티티 변경 시 스키마 업데이트 (개발용) # - validate: 엔티티와 스키마 일치 여부만 검증 (운영용) # - none: 아무 작업도 하지 않음 -spring.jpa.hibernate.ddl-auto=update +spring.jpa.hibernate.ddl-auto=none +spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create +spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=ddl/schema.sql +spring.jpa.properties.hibernate.hbm2ddl.schema-generation.script.append=false -# SQL 쿼리를 콘솔에 표시 -spring.jpa.show-sql=true - -# Hibernate가 SQL을 포맷하여 보여줌 +# Hibernate log +spring.jpa.show-sql=false spring.jpa.properties.hibernate.format_sql=true +spring.jpa.properties.hibernate.highlight_sql=true +spring.jpa.properties.hibernate.use_sql_comments=false +logging.level.org.hibernate.SQL=DEBUG +logging.level.org.hibernate.orm.jdbc.bind=TRACE +logging.level.org.springframework.data.jpa=DEBUG + +# P6Spy +decorator.datasource.p6spy.enable-logging=true +decorator.datasource.p6spy.log-format=%(sqlSingleLine) + +# CONSOLE +spring.output.ansi.enabled=always # HikariCP 연결 풀 크기 설정 (선택사항) # spring.datasource.hikari.maximum-pool-size=10