๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ–ฅ๏ธ ๋ฐฑ์—”๋“œ/SpringBoot

[Querydsl] DTO ์กฐํšŒ ๋ฐฉ๋ฒ•

by OR15A 2024. 1. 7.
์ˆœ์ˆ˜ JPA์—์„œ DTO ์กฐํšŒ
List<MemberDto> result = em.createQuery(
                         "select new study.querydsl.dto.MemberDto(m.username, m.age) " +
                         "from Member m", MemberDto.class)
                         .getResultList();
  • new ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•จ
  • DTO์˜ package์ด๋ฆ„์„ ๋‹ค ์ ์–ด์ค˜์•ผํ•จ
  • ์ƒ์„ฑ์ž ๋ฐฉ์‹๋งŒ ์ง€์›ํ•จ

 

Querydsl ๋นˆ ์ƒ์„ฑ(Bean population)
  • ๊ฒฐ๊ณผ๋ฅผ DTO ๋ฐ˜ํ™˜ํ•  ๋•Œ ์‚ฌ์šฉํ•จ
  • 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ง€์›ํ•จ
    • ํ”„๋กœํผํ‹ฐ ์ ‘๊ทผ
    • ํ•„๋“œ ์ง์ ‘ ์ ‘๊ทผ
    • ์ƒ์„ฑ์ž ์‚ฌ์šฉ

 

[ํ”„๋กœํผํ‹ฐ ์ ‘๊ทผ - Setter]
List<MemberDto> result = queryFactory
                         .select(Projections.bean(MemberDto.class,
                                                     member.username,
                                                     member.age))
                         .from(member)
                         .fetch();
[ํ•„๋“œ ์ง์ ‘ ์ ‘๊ทผ]
List<MemberDto> result = queryFactory
                         .select(Projections.fields(MemberDto.class,
                                                     member.username,
                                                     member.age))
                         .from(member)
                         .fetch();
[ํ•„๋“œ ์ง์ ‘ ์ ‘๊ทผ-๋ณ„์นญ์ด ๋‹ค๋ฅผ ๋•Œ]
List<UserDto> fetch = queryFactory
                     .select(Projections.fields(UserDto.class, member.username.as("name"),
                             ExpressionUtils.as( JPAExpressions
                                                 .select(memberSub.age.max())
                                                 .from(memberSub), "age")
                             )
                     ).from(member)
                     .fetch();
[์ƒ์„ฑ์ž ์‚ฌ์šฉ]
List<MemberDto> result = queryFactory
                         .select(Projections.constructor(MemberDto.class,
                                                         member.username,
                                                         member.age))
                         .from(member)
                         .fetch();

 

 

@QueryProjection ํ™œ์šฉ
[Dto ์ƒ์„ฑ์ž์— @QueryProjection ์ถ”๊ฐ€] 
...
@QueryProjection
 public MemberDto(String username, int age) {
         this.username = username;
         this.age = age;
 }
 ...
 
 
 [@QueryProjection ํ™œ์šฉ]
 List<MemberDto> result = queryFactory
                         .select(new QMemberDto(member.username, member.age))
                         .from(member)
                         .fetch();
  • ์žฅ์ : ์ปดํŒŒ์ผ๋Ÿฌ๋กœ ํƒ€์ž… ์ฒดํฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ์Œ
  • ์ฃผ์˜์ : ๋‹ค๋งŒ DTO์— Querydsl ์• ๋„ˆํ…Œ์ด์…˜์„ ์œ ์ง€ํ•ด์•ผํ•˜๋ฉฐ, DTO๊นŒ์ง€ QํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์•ผ ํ•จ.
    • DTO๊ฐ€ Querydsl์— ์˜์กด์„ฑ์„ ๊ฐ€์ง. ์„œ๋น„์Šค, ์ปจํŠธ๋กค๋Ÿฌ ๊ณ„์ธต์—์„œ๋„ ์‚ฌ์šฉ๋˜๋Š” DTO๋กœ์„œ๋Š” ๊น”๋”ํ•˜์ง€ ๋ชปํ•œ ๋ฐฉ๋ฒ•.

 

 

 

 

์กฐํšŒ์ฟผ๋ฆฌ๊ฐ€ ๋„ˆ๋ฌด ๋ณต์žกํ•  ๋•Œ
  • ํŠน์ •ํ•œ ๊ธฐ๋Šฅ์— ๋งž์ถฐ์ง„ ์กฐํšŒ ๊ธฐ๋Šฅ์ผ ๊ฒฝ์šฐ์—๋Š” ๋”ฐ๋กœ MemberQueryRepository๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ฃผ์ž… ๋ฐ›์•„์„œ ์‚ฌ์šฉํ•˜๊ธฐ.