Notice
Recent Posts
Recent Comments
Link
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

검색조건 쿼리 본문

인프런/[인프런] QueryDsl

검색조건 쿼리

Seung__ 2021. 9. 1. 23:12

1. where문에 and나 or 사용 가능


  • selectFrom로 select와 from 합칠 수 있음 ㅋㅋ
 @Test
    public void search(){
        Member findMember = queryFactory
                .selectFrom(member)
                .where(member.username.eq("member1").and(member.age.eq(10)))
                .fetchOne();
        assertThat(findMember.getUsername()).isEqualTo("member1");
    }

 

2. JPQL이 제공하는 모든 검색 조건 제공


member.username.eq("member1") // username = 'member1'
member.username.ne("member1") //username != 'member1'
member.username.eq("member1").not() // username != 'member1'
member.username.isNotNull() //이름이 is not null
member.age.in(10, 20) // age in (10,20)
member.age.notIn(10, 20) // age not in (10, 20)
member.age.between(10,30) //between 10, 30
member.age.goe(30) // age >= 30
member.age.gt(30) // age > 30
member.age.loe(30) // age <= 30
member.age.lt(30) // age < 30
member.username.like("member%") //like 검색
member.username.contains("member") // like ‘%member%’ 검색
member.username.startsWith("member") //like ‘member%’ 검색
...

 

 

3. AND 조건을 파라미터로 처리


  • where() 에 파라미터로 검색조건을 추가하면 AND 조건이 추가됨
  • 이 경우 null 값은 무시 메서드 추출을 활용해서 동적 쿼리를 깔끔하게 만들 수 있음 뒤에서 설명
 @Test
    public void searchAndParam(){
        Member findMember = queryFactory
                .selectFrom(member)
                .where(
                        member.username.eq("member1"),
                        (member.age.eq(10)))
                
                .fetchOne();
        assertThat(findMember.getUsername()).isEqualTo("member1");
    }

 

4. GitHub : 210901 Search


 

GitHub - bsh6463/Querydsl

Contribute to bsh6463/Querydsl development by creating an account on GitHub.

github.com

 

'인프런 > [인프런] QueryDsl' 카테고리의 다른 글

정렬  (0) 2021.09.01
결과 조회  (0) 2021.09.01
기본 Q-Type  (0) 2021.09.01
Querydsl vs JPQL  (0) 2021.09.01
예제 도메인 모델  (0) 2021.09.01
Comments