[TIL] QueryDSL๐Ÿ‘€โ“

sorzzzzyยท2022๋…„ 1์›” 28์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
22/36
post-thumbnail

๊ฐ์ฒด์ง€ํ–ฅ ์ฟผ๋ฆฌ ์–ธ์–ด๋ฅผ ๊ณต๋ถ€ํ•˜๋‹ค๊ฐ€ ๋ฌธ๋“, JPQL ๊ณผ QueryDSL ์˜ ์ฐจ์ด์ ์ด ๊ถ๊ธˆํ•ด์กŒ๋‹ค!

๐Ÿ“Œ JPQL ์ฐธ๊ณ 

JPQL ์— ๋Œ€ํ•ด์„œ๋Š” ๐Ÿ‘†๐Ÿป์ด์ „์— ์ž‘์„ฑํ•œ ํฌ์ŠคํŒ…๐Ÿ‘†๐Ÿป์ด ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์ž!


Spring Data JPA๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•ด์ฃผ๋Š” CRUD ๋ฉ”์†Œ๋“œ ๋ฐ ์ฟผ๋ฆฌ ๋ฉ”์†Œ๋“œ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋”๋ผ๋„,
์›ํ•˜๋Š” ์กฐ๊ฑด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•„์—ฐ์ ์œผ๋กœ JPQL์„ ์ž‘์„ฑํ•˜๊ฒŒ ๋œ๋‹ค.

๊ฐ„๋‹จํ•œ ๋กœ์ง์„ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ดœ์ฐฎ์ง€๋งŒ, ๋ณต์žกํ•œ ๋กœ์ง์˜ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ์—๋Š” ๋ฌธ์ž์—ด์ด ๊ธธ์–ด์ง€๊ฒŒ ๋œ๋‹ค.

@Test
public void testJPQL() {
	String qlString = "select m from Member m " +
			"where m.userName = :userName";

	Member findMember = em.createQuery(qlString, Member.class)
				.setParameter("userName", "kang")
				.getSingleResult();

	assertThat(findMember.getUserName()).isEqualTo("kang");
}

JPQL ์€ ๋ฌธ์ž์—ด์œผ๋กœ Type-Check๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
ํ•ด๋‹น ๋กœ์ง์„ ์‹คํ–‰ํ•˜๊ธฐ ์ „๊นŒ์ง€๋Š” ์ž‘๋™์—ฌ๋ถ€ ํ™•์ธ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๊ณ , ์‹คํ–‰ ์‹œ์ ์ด ๋˜์–ด์„œ์•ผ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ๊ฒฌ๋œ๋‹ค!


์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด ์ฃผ๋Š” ๊ฒƒ์ด QueryDSL์ด๋‹ค!
QueryDSL ์€ ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ์ž๋ฐ” ์ฝ”๋“œ๋กœ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ปดํŒŒ์ผ ์‹œ์ ์— ๋ฌธ๋ฒ•์  ์˜ค๋ฅ˜๋ฅผ ์‰ฝ๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค!
๋˜ํ•œ ์ž๋™์™„์„ฑ ๋“ฑ IDE์˜ ๋„์›€์„ ๋ฐ›์„ ์ˆ˜๋„ ์žˆ๊ณ , ๋™์  ์ฟผ๋ฆฌ ์ž‘์„ฑ์ด ํŽธ๋ฆฌํ•˜๋‹ค.

@Test
public void testQuerydsl() {
	queryFactory = new JPAQueryFactory(em);
	QMember m = new QMember("m");

	Member findMember = queryFactory
			.select(m)
			.from(m)
			.where(m.userName.eq("kang"))
			.fetchOne();

	assertThat(findMember.getUserName()).isEqualTo("kang");

}

QueryDSL ๋ฌธ๋ฒ•์€ ๋˜ํ•œ SQL ๊ณผ ๊ต‰์žฅํžˆ ๋น„์Šทํ•œ ๊ตฌ์กฐ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.
์ง€๋ฃจํ•œ ๋ฐ˜๋ณต ์ž‘์—…์„ ์ค„์—ฌ์ฃผ๋Š” QueryDSL ์— ๋Œ€ํ•ด ์•ž์œผ๋กœ๋„ ๊พธ์ค€ํžˆ ๊ณต๋ถ€ํ•ด๋ด์•ผ๊ฒ ๋‹ค๐Ÿ˜Š

profile
Backend Developer

0๊ฐœ์˜ ๋Œ“๊ธ€