JPA

[ Querydsl ] ๋™์ ์ฟผ๋ฆฌ ๋งŒ๋“ค๊ธฐ(BooleanExpression, BooleanBuilder)

PYT 2022. 3. 22. 01:26
๋ฐ˜์‘ํ˜•

Querydsl์—์„œ ๋™์  ์ฟผ๋ฆฌ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•

Querydsl์—์„œ๋Š” ๋™์ ์ฟผ๋ฆฌ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด BooleanBuilder์™€ Where ๋‹ค์ค‘ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์ด ์žˆ๋‹ค.

๋‘˜๋‹ค ๊ฐ™์€ 'Expression' ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ๋•Œ๋ฌธ์— Where๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค๋Š” ๋ชฉ์ ์€ ๊ฐ™์ง€๋งŒ, ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ์‹์— ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.


1. BooleanBuilder

 

@Test
public void ๋™์ ์ฟผ๋ฆฌ_BooleanBuilder() throws Exception {
        String usernameParam = "member1";
        Integer ageParam = 10;
        List<Member> result = searchMember1(usernameParam, ageParam);
        Assertions.assertThat(result.size()).isEqualTo(1);
}

private List<Member> searchMember1(String usernameCond, Integer ageCond) {
        BooleanBuilder builder = new BooleanBuilder();
        if (usernameCond != null) {
            builder.and(member.username.eq(usernameCond));
        }
        if (ageCond != null) {
    		builder.and(member.age.eq(ageCond));
		}
        return queryFactory
                .selectFrom(member)
                .where(builder)
                .fetch();
}

 

BooleanBuilder๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ€๋…์„ฑ๋„ ์ข‹์ง€ ์•Š๊ณ  ์ฝ”๋“œ๋ฅผ ํŒŒ์•…ํ•˜๋Š”๋ฐ ์‹œ๊ฐ„๋„ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๊ฒŒ ๋˜๋ฉฐ ์–ด๋–ค ์ฟผ๋ฆฌ๊ฐ€ ๋‚˜๊ฐˆ์ง€ ์˜ˆ์ธก์ด ํž˜๋“ค๋‹ค.

๋˜ํ•œ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ์กฐ๊ฑด๋ฌธ์„ ํ†ตํ•ด null ์ฒดํฌ๋ฅผ ํ•ด์ค˜์•ผํ•จ์€ ๋ฌผ๋ก  and๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ํ•˜๋‚˜ํ•˜๋‚˜ ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

ํ•˜๋‚˜์˜ ์กฐ๊ฑด์ผ ๋•Œ์•ผ ๋‹จ์ˆœํ•˜๊ฒ ์ง€๋งŒ ์ ˆ๋Œ€ ๊ทธ๋Ÿด์ผ์€์—†์„ ํ…Œ๊ณ , ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋งŽ์•„์ง์— ๋”ฐ๋ผ ์ฝ”๋“œ์˜ ์–‘์ด ์—„์ฒญ๋‚˜๊ฒŒ ๋Š˜์–ด๋‚˜๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค.


2. BooleanExpression

 

์ด๋Ÿฌํ•œ BooleanBuilder์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด BooleanExpression์„ ์‚ฌ์šฉํ•˜์ž.

@Test
public void ๋™์ ์ฟผ๋ฆฌ_WhereParam() throws Exception { String usernameParam = "member1";
Integer ageParam = 10;
      List<Member> result = searchMember2(usernameParam, ageParam);
      Assertions.assertThat(result.size()).isEqualTo(1);
  }
  private List<Member> searchMember2(String usernameCond, Integer ageCond) {
      return queryFactory
              .selectFrom(member)
              .where(usernameEq(usernameCond), ageEq(ageCond))
              .fetch();
  }
  private BooleanExpression usernameEq(String usernameCond) {
      return usernameCond != null ? member.username.eq(usernameCond) : null;
  }
  private BooleanExpression ageEq(Integer ageCond) {
      return ageCond != null ? member.age.eq(ageCond) : null;
}

 

 BooleanExpression์€ ์˜ˆ์‹œ์ฝ”๋“œ์ฒ˜๋Ÿผ ๋ฉ”์†Œ๋“œ๋ฅผ ๋”ฐ๋กœ ๋บ๊ธฐ๋•Œ๋ฌธ์—, ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋กœ์ง์ด ์œ„์— ์˜ค๊ฒŒ๋˜์–ด ๊ฐ€๋…์„ฑ์ด ๋†’์•„์ง€๊ณ  ๋ฉ”์†Œ๋“œ ๋ช…์„ ํ†ตํ•ด ๊ตณ์ด ๋ฉ”์†Œ๋“œ์˜ ๋‚ด์šฉ์„ ๋ณด์ง€ ์•Š๋”๋ผ๋„ ์‰ฝ๊ฒŒ ์–ด๋–ค ๋‚ด์šฉ์ธ์ง€ ํŒŒ์•…์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

๋˜ํ•œ null์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋˜๋ฉด Where์ ˆ์—์„œ ์กฐ๊ฑด์€ ๋ฌด์‹œ๋˜์–ด ์•ˆ์ „ํ•˜๊ณ , BooleanExpression์„ ํ™œ์šฉํ•˜๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ and์™€ or๊ฐ™์€ ๋ฉ”์†Œ๋“œ๋“ค์„ ์ด์šฉํ•ด BooleanExpression๋“ค์„ ์กฐํ•ฉํ•ด ์ƒˆ๋กœ์šด  BooleanExpression์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์–ด ์žฌ์‚ฌ์šฉ์„ฑ์ด ๋†’๋‹ค.
 
 private BooleanExpression allEq(String usernameCond, Integer ageCond) {
      return usernameEq(usernameCond).and(ageEq(ageCond));
}

 

์šฐ์•„ํ•œ ํ˜•์ œ๋“ค์˜ Querydsl ํ™œ์šฉ๋ฒ•์—์„œ๋„ ๋™์ ์ฟผ๋ฆฌ๋Š” BooleanExpression์„ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ๋‚˜์™€์žˆ๋‹ค.

 

์šฐ์•„ํ•œ ํ˜•์ œ๋“ค์˜ Querydsl ์‚ฌ์šฉ๋ฒ•

์ด ๊ธ€์€ "์šฐ์•„ํ•œํ…Œํฌ์ฝ˜์„œํŠธ2020 ์ˆ˜์‹ญ์–ต๊ฑด์—์„œ Querydsl ์‚ฌ์šฉํ•˜๊ธฐ" ์™€ ๋ฐœํ‘œ์ž์ด์‹  ์ด๋™์šฑ๋‹˜์˜ ๊ธฐ์ˆ  ๋ธ”๋กœ๊ทธ๋ฅผ ๋ณด๊ณ  ์ž‘์„ฑํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ๋ชจ๋“  ์˜ˆ์ œ์™€ ์ถ”๊ฐ€๋กœ Querydsl ์‚ฌ์šฉ ๋ฌธ๋ฒ•์€ https://github.com/Youngerjesus/Q

velog.io

 

๊ฒฐ๋ก ์€ ๋™์ ์ฟผ๋ฆฌ์‹œ BooleanExpression์„ ์‚ฌ์šฉํ•˜์ž ! 

 

1) ๊ฐ€๋…์„ฑ์ด ๋›ฐ์–ด๋‚จ

2) null ๋ฐ˜ํ™˜ ์‹œ Where์ ˆ์—์„œ ๋ฌด์‹œ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์•ˆ์ „

3) and, or๊ฐ™์€ ๋ฉ”์†Œ๋“œ๋“ค์„ ์ด์šฉํ•ด BooleanExpression์„ ์กฐํ•ฉํ•ด ์ƒˆ๋กœ์šด BooleanExpression๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์–ด ์žฌ์‚ฌ์šฉ์„ฑ์ด ์ข‹๊ธฐ ๋•Œ๋ฌธ์— !


 

[Querydsl] ๋‹ค์ด๋‚˜๋ฏน ์ฟผ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ

์•ˆ๋…•ํ•˜์„ธ์š”! ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” Querydsl์—์„œ์˜ ๋‹ค์ด๋‚˜๋ฏน ์ฟผ๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ์ž‘์„ฑํ•˜๋ฉด ์ข‹์„์ง€์— ๋Œ€ํ•ด ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ฒ˜์Œ Querydsl์„ ์“ฐ์‹œ๋Š” ๋ถ„๋“ค์ด ๊ฐ€์žฅ ๋งŽ์ด ์‹ค์ˆ˜ํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ˆ ๊ทธ๋Ÿผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค! ๋ชจ๋“  ์ฝ”๋“œ

jojoldu.tistory.com

๋ฐ˜์‘ํ˜•