[QueryDSL] 세팅

Jimin Lim·2022년 2월 2일

JPA

목록 보기
1/2
post-thumbnail

QueryDSL

사용하는 이유

  • 문자가 아닌 코드로 작성하기 때문에 컴파일 시점에 문법 오류를 찾을 수 있다.
  • 동적 쿼리 작성이 편리하다.

환경 설정

  • command + , → Annotation Processors → Enable annotation processing

  • command + , → Gradle → IntelliJ 로 변경

  • build.gradleplugins

    //querydsl 추가
    		id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
  • build.gradledependencies

    //querydsl 추가
    implementation 'com.querydsl:querydsl-jpa'
  • build.gradle

    //젤 위에 추가  
    buildscript {
      ext {
        queryDslVersion = "5.0.0"
      }
    }
    //querydsl 추가 시작
    def querydslDir = "$buildDir/generated/querydsl"
    querydsl {
      jpa = true
      querydslSourcesDir = querydslDir
    }
    sourceSets {
      main.java.srcDir querydslDir
    }
    configurations {
      querydsl.extendsFrom compileClasspath
    }
    compileQuerydsl {
      options.annotationProcessorPath = configurations.querydsl
    }
    //querydsl 추가 끝
    
  • gradleTasksothercompileQuerydsl 클릭

H2 데이터베이스 세팅

: 개발이나 테스트 용도로 가볍고 편리한 DB, 웹 화면을 제공한다.

1. 최소 한번 데이터베이스 생성

jdbc:h2:~/querydsl

2. 이후 접속

jdbc:h2:tcp://localhost/~/querydsl

3. application.yml 설정

spring:
  datasource:
    url: jdbc:h2:tcp://localhost/~/querydsl
    username: sa
    password:
    driver-class-name: org.h2.Driver

  jpa:
    hibernate:
      ddl-auto: create
    properties:
      hibernate:
        #show_sql: true 
        format_sql: true

logging.level:
  org.hibernate.SQL: debug
#  org.hibernate.type: trace

SQL 로그 확인

  • show_sql: sout으로 출력
  • format_sql: debug logger로 출력, sql문에 ?이 아닌 구체적인 값을 확인하고 싶다면 logging.levelorg.hibernate.type: trace를 설정해준다.
  • 쿼리 파라미터 로그:
    implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.5.8'
    • 실행 파라미터를 로그로 남기고 싶다면 외부 라이브러리를 추가하도록 한다. 이때, 실제 운영에 사용할 때 성능 테스트를 해보는 것을 권장한다.
profile
💻 ☕️ 🏝 🍑 🍹 🏊‍♀️

0개의 댓글