DOing
Querydsl์ด๋? ๋ณธ๋ฌธ
๐ฑ Querydsl?
์ฟผ๋ฆฌ๋ฅผ ์๋ฐ์ฝ๋๋ก ์์ฑํ ์ ์๊ฒ ๋์์ฃผ๋ ๊ธฐ์ ์ด๋ค.
Spring Data JPA๋ก ํด๊ฒฐํ์ง ๋ชปํ๋ ๋ณต์กํ ์ฟผ๋ฆฌ/๋์ ์ฟผ๋ฆฌ๋ฅผ ํด๊ฒฐํ ์ ์๋ค.
์๋ฐ์ฝ๋๋ก ์์ฑํ๊ธฐ ๋๋ฌธ์ ๋ฌธ๋ฒ์ค๋ฅ๋ฅผ ์ปดํ์ผ ์์ ์ ์ก์๋ผ ์ ์๋ค.
๐ฑ build.gradle์์ Querydsl ์ค์
plugins {
id 'org.springframework.boot' version '2.5.3'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
//querydsl ์ถ๊ฐ
id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
//querydsl ์ถ๊ฐ
implementation 'com.querydsl:querydsl-jpa'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
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๋ก ์์ฑํ ์ฝ๋๋ JPQL์ด ๋๋ค๊ณ ๋ณด๋ฉด ๋๋ค.
์คํ๋๋ JPQL์ ๋ณด๊ณ ์ถ๋ค๋ฉด application.properties์ ๋ค์๊ณผ ๊ฐ์ด ์ถ๊ฐํ๋ค.
spring.jpa.properties.hibernate.use_sql_comments: true
๐ฑ Querydsl์์ ์๋ ์์ฑํด์ค QEntity
Querydsl์ด ์ง์ ๋ง๋ hello๋ผ๋ Entity๋ฅผ ๋ณด๊ณ QEntity๋ฅผ ๋ง๋ค์๋ค.
์ฃผ์ํ ์ ์ generated๋ Qํ์ผ๋ค์ git์๋ค๊ฐ ์ฌ๋ฆฌ๋ฉด ์๋๋ค๋ ์ ์ด๋ค.
์๋ํ๋ฉด ์์ฑ๋ Qํ์ผ๋ค์ ์์คํ ์ด ์๋์ผ๋ก ์์ฑํด์ฃผ๋ ๊ฒ์ด๋ผ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฒ์ ์ด ์ฌ๋ผ๊ฐ๊ฒ๋๋ฉด ์์ฑ๋ ์ธ๋ถ ๋ด์ฉ๋ค์ด ๋ณ๊ฒฝ๋ ์ ์์ด์ ์ด๋ฐ ๊ฒ๋ค์ด git์ ์ฌ๋ผ๊ฐ๋ฉด ์๋๋ค. ์ด๋ฐ ํ์ผ๋ค์ gitignore๋ฅผ ์ง์ ์ค์ ํ ์๋ ์์ง๋ง ์ ์ด์ ํ์ผ ์์ฑ๋๋ ๊ฒฝ๋ก๋ฅผ build๋ก ํด์ฃผ๋ฉด ํธํ๋ค. ๋ณดํต buildํด๋๋ ๊ธฐ๋ณธ์ผ๋ก gitignore์ ๋์ด์๊ธฐ ๋๋ฌธ์ด๋ค.
๐ฑ QHello.java
/**
* QHello is a Querydsl query type for Hello
*/
@Generated("com.querydsl.codegen.EntitySerializer")
public class QHello extends EntityPathBase<Hello> {
private static final long serialVersionUID = -1579446036L;
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);
}
}
๐ฑ JPQL vs Querydsl ๋น๊ต
์ฟผ๋ฆฌ๋ฅผ ์๋ฐ์ฝ๋๋ก ์์ฑํ ๊ฒ์ ๋ณผ ์ ์๋ค. ์ด ๋๋ถ์ ๋ฌธ๋ฒ์ค๋ฅ๋ฅผ ์ปดํ์ผ ์์ ์ ์ก์๋ผ ์ ์๋ค.
import static com.example.inflearnquerydsl.entity.QMember.member;
@SpringBootTest
@Transactional
public class QuerydslBasicTest {
@PersistenceContext
EntityManager em;
JPAQueryFactory queryFactory; // ๋์์ฑ ๋ฌธ์ ?
// ์คํ๋ง ํ๋ ์์ํฌ๋ ์ฌ๋ฌ ์ฐ๋ ๋์์ ๋์์ ๊ฐ์ EntityManager์ ์ ๊ทผํด๋,
// ํธ๋์ญ์
๋ง๋ค ๋ณ๋์ ์์์ฑ ์ปจํ
์คํธ๋ฅผ ์ ๊ณตํ๊ธฐ ๋๋ฌธ์, ๋์์ฑ ๋ฌธ์ ๋ ๊ฑฑ์ ํ์ง ์์๋ ๋๋ค.
@BeforeEach
public void before() {
queryFactory = new JPAQueryFactory(em);
Team teamA = new Team("teamA");
Team teamB = new Team("teamB");
em.persist(teamA);
em.persist(teamB);
Member member1 = new Member("member1", 10, teamA);
Member member2 = new Member("member2", 20, teamA);
Member member3 = new Member("member3", 30, teamB);
Member member4 = new Member("member4", 40, teamB);
em.persist(member1);
em.persist(member2);
em.persist(member3);
em.persist(member4);
}
@Test
public void startJPQL() {
//member1์ ์ฐพ์๋ผ.
String qlString =
"select m from Member m " +
"where m.username = :username";
Member findMember = em.createQuery(qlString, Member.class)
.setParameter("username", "member1")
.getSingleResult();
assertThat(findMember.getUsername()).isEqualTo("member1");
}
@Test
public void startQuerydsl() {
//member1์ ์ฐพ์๋ผ.
Member findMember = queryFactory
.select(member)
.from(member)
.where(member.username.eq("member1"))//ํ๋ผ๋ฏธํฐ ๋ฐ์ธ๋ฉ ์ฒ๋ฆฌ
.fetchOne();
assertThat(findMember.getUsername()).isEqualTo("member1");
}
}
'JPA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Querydsl] ์ ๋ ฌ, ํ์ด์ง, ์ง๊ณ ํจ์, ๊ทธ๋ฃน ํจ์ (0) | 2021.08.04 |
---|---|
[Querydsl] where ์กฐ๊ฑด๊ณผ ๊ฒฐ๊ณผ ์กฐํ (0) | 2021.08.04 |
[JPA] ์ฐ์์ฑ ์ ์ด(CASCADE) (0) | 2021.07.15 |
[JPA] Fetch - ์ฆ์ ๋ก๋ฉ, ์ง์ฐ ๋ก๋ฉ (0) | 2021.07.14 |
[JPA] ์์์ฑ ์ปจํ ์คํธ3 - ํ๋ฌ์ (0) | 2021.07.14 |