๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐‘†๐‘ข๐‘›๐‘ โ„Ž๐‘–๐‘›๐‘’ ๐‘Ž๐‘“๐‘ก๐‘’๐‘Ÿ ๐‘Ÿ๐‘Ž๐‘–๐‘›โœง

[Spring] QueryDSL ๋ณธ๋ฌธ

QueryDSL

  • ์ •์ž‘ ํƒ€์ž…์„ ์ด์šฉํ•ด SQL๊ณผ ๊ฐ™์€ ์ฟผ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•˜๋Š” ํ”„๋ ˆ์ž„์›Œํฌ
  • ์ฟผ๋ฆฌ๋ฅผ ๋ฌธ์ž๊ฐ€ ์•„๋‹Œ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•ด๋„, ์‰ฝ๊ณ  ๊ฐ„๊ฒฐํ•˜๋ฉฐ ๊ทธ ๋ชจ์–‘๋„ ์ฟผ๋ฆฌ์™€ ๋น„์Šทํ•˜๊ฒŒ ๊ฐœ๋ฐœํ• ์ˆ˜ ์žˆ๋Š” ์˜คํ”ˆ์†Œ์Šค ํ”„๋กœ์ ํŠธ

 

QueryDSL์˜ ์žฅ์ 

  • IDE๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์ฝ”๋“œ ์ž๋™ ์™„์„ฑ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฌธ๋ฒ•์ ์œผ๋กœ ์ž˜๋ชป๋œ ์ฟผ๋ฆฌ๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ ์ •์ƒ์ ์œผ๋กœ ํ™œ์šฉ๋œ QueryDSL์€ ๋ฌธ๋ฒ• ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š๋Š”๋‹ค.
  • ๊ณ ์ •๋œ SQL ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋™์ ์œผ๋กœ ์ฟผ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. 
  • ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•˜๋ฏ€๋กœ ๊ฐ€๋…์„ฑ ๋ฐ ์ƒ์‚ฐ์„ฑ์ด ํ–ฅ์ƒ๋œ๋‹ค. 
  • ๋„๋ฉ”์ธ ํƒ€์ž…๊ณผ ํ”„๋กœํผํ‹ฐ๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ์ฐธ์กฐํ• ์ˆ˜ ์žˆ๋‹ค.

 

QueryDSL ์„ค์ •

 

buildscript {
    ext {
        queryDslVersion = "5.0.0"
    }
}

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.7.13'
    id 'io.spring.dependency-management' version '1.0.15.RELEASE'
    //querydsl ์ถ”๊ฐ€
    id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
}

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

java {
    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-web'
    compileOnly 'org.projectlombok:lombok'
    runtimeOnly 'com.mysql:mysql-connector-j'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    //querydsl ์ถ”๊ฐ€
    //querydsl ์ถ”๊ฐ€
    implementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
    annotationProcessor "com.querydsl:querydsl-apt:${queryDslVersion}"
}

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

//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 ์ถ”๊ฐ€ ๋

 

ํ™˜๊ฒฝ ์„ค์ •์„ ์™„๋ฃŒํ–ˆ์œผ๋ฉด Gradle -> Tasks -> other -> compileQuerydsGradle๋ฅผ ๋ˆŒ๋Ÿฌ Qํƒ€์ž…์„ ์ƒ์„ฑํ•ด์ค€๋‹ค.

Q ํƒ€์ž… ์ƒ์„ฑ๋๋Š”์ง€ ํ™•์ธํ• ๋ ค๋ฉด build -> generated -> querydsl -> example.querydsl.entity.QHello.java ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

 

package com.example.querydsl.entity;

import static com.querydsl.core.types.PathMetadataFactory.*;

import com.querydsl.core.types.dsl.*;

import com.querydsl.core.types.PathMetadata;
import javax.annotation.processing.Generated;
import com.querydsl.core.types.Path;


/**
 * QHello is a Querydsl query type for Hello
 */
@Generated("com.querydsl.codegen.DefaultEntitySerializer")
public class QHello extends EntityPathBase<Hello> {

    private static final long serialVersionUID = -932168945L;

    public static final QHello hello = new QHello("hello");

    public final NumberPath<Long> id = createNumber("id", Long.class);

    public QHello(String variable) {
        super(Hello.class, forVariable(variable));
    }

    public QHello(Path<? extends Hello> path) {
        super(path.getType(), path.getMetadata());
    }

    public QHello(PathMetadata metadata) {
        super(Hello.class, metadata);
    }

}

 

ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ์‹คํ–‰ ๊ฒ€์ฆ

 

package com.example.querydsl;

import com.example.querydsl.entity.Hello;
import com.example.querydsl.entity.QHello;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
@Transactional
class QuerydslApplicationTests {

    @Autowired
    EntityManager em;

    @Test
    void contextLoads() {
        Hello hello = new Hello();
        em.persist(hello);

        JPAQueryFactory query = new JPAQueryFactory(em);
        QHello qHello = new QHello("h");

        Hello result = query
                .selectFrom(qHello)
                .fetchOne();
        // ctrl + alt + v

        assertThat(result).isEqualTo(hello);
        Assertions.assertThat(result.getId()).isEqualTo(hello.getId());
    }

}

 

ํ…Œ์ŠคํŠธ ํ†ต๊ณผ