자바생
article thumbnail
Published 2022. 5. 22. 17:39
Querydsl의 like vs contains Spring Data
728x90

yapp에서 주어진 단어가 게시글의 제목에 포함되있는지 판단하기 위해 like을 알게 됐다

하지만 like가 아닌 contains도 있었고, 이 둘의 차이가 무엇인지 궁금해서 테스트를 통해 확인해봤다

 

결론

  • like(str)은 쿼리가 나갈 때 str자체가 나간다
    • 즉, 정확히 일치해야한다 
    • like는 내가 % 연산을 선택할 수 있다
  • contains(str)은 쿼리가 나갈 때 %str%가 나간다

 

like

현재 Member 에는 member1, aamember1, aamember1aa, member1aa가 저장되어있다

@Test
@DisplayName("like 확인")
void like() throws Exception {

    List<Member> result1 = queryFactory
            .selectFrom(member)
            .where(member.username.like("member1"))
            .fetch();
    for (Member member : result1) {
        System.out.println("member = " + member);
    }
    System.out.println("================");
//////////////////////////////////////////////////////
    List<Member> result2 = queryFactory
            .selectFrom(member)
            .where(member.username.like("%member1"))
            .fetch();
    for (Member member : result2) {
        System.out.println("member = " + member);
    }
    System.out.println("================");
//////////////////////////////////////////////////////
    List<Member> result3 = queryFactory
            .selectFrom(member)
            .where(member.username.like("member1%"))
            .fetch();

    for (Member member : result3) {
        System.out.println("member = " + member);
    }
    System.out.println("================");
//////////////////////////////////////////////////////
    List<Member> result4 = queryFactory
            .selectFrom(member)
            .where(member.username.like("%member1%"))
            .fetch();

    for (Member member : result4) {
        System.out.println("member = " + member);
    }
    System.out.println("================");
}

 

1번째 쿼리

  • like("member1")
  • 쿼리를 보면 정확히 member1이 일치하는 경우만 가져온다

 

2번째 쿼리

 

  • like("%member1)
  • 쿼리를 보면 하위str + member1인 경우만 가져온다

 

3번째 쿼리

  • like("member1%)
  • 쿼리를 보면 member1 + 상위str 인 경우만 가져온다

 

4번째 쿼리

 

  • like("%member1%)
  • member1을 포함한 경우를 가져온다

 

contains

 

@Test
@DisplayName("contains 확인")
void contains() throws Exception {
    List<Member> result1 = queryFactory
            .selectFrom(member)
            .where(member.username.contains("member1"))
            .fetch();

    for (Member member : result1) {
        System.out.println("member = " + member);
    }
    System.out.println("================");

    List<Member> result2 = queryFactory
            .selectFrom(member)
            .where(member.username.contains("member1"))
            .fetch();

    for (Member member : result2) {
        System.out.println("member = " + member);
    }
    System.out.println("================");

    List<Member> result3 = queryFactory
            .selectFrom(member)
            .where(member.username.contains("member1"))
            .fetch();
    for (Member member : result3) {
        System.out.println("member = " + member);
    }
    System.out.println("================");

    List<Member> result4 = queryFactory
            .selectFrom(member)
            .where(member.username.contains("member1"))
            .fetch();

    for (Member member : result4) {
        System.out.println("member = " + member);
    }
    System.out.println("================");
}

  • contains는 모든 쿼리가 %member1%로 나가는 것을 알 수 있다

 

startsWith & endsWith

  • startsWith은 member1%
  • endsWith은 %member1

728x90
profile

자바생

@자바생

틀린 부분이 있다면 댓글 부탁드립니다~😀

검색 태그