검색 조건 쿼리
- 사실 동적 쿼리 Querydsl로 짜고 싶어서 배우는 것!..
@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");
}
- 쿼리를 생각해보면
- select * from Member m where m.username = 'member1' and m.age = 10; 이런 쿼리이다.
/* select
member1
from
Member member1
where
member1.username = ?1
and member1.age = ?2 */
select
m1_0.member_id,
m1_0.age,
m1_0.team_id,
m1_0.username
from
member m1_0
where
m1_0.username=?
and m1_0.age=?
- 내 예상대로 나온 것을 확인할 수 있다.
JPQL이 제공하는 모든 검색 조건 제공
board.writer.eq("writer1") // writer = 'writer1'
board.writer.ne("writer1") //writer != 'writer1'
board.writer.eq("writer1").not() // writer != 'writer1'
board.writer.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
board.writer.like("writer%") //like 검색
board.writer.contains("writer") // like ‘%writer%’ 검색
board.writer.startsWith("writer") //like ‘writer%’ 검색
@Test
public void search() {
Member findMember = queryFactory
.selectFrom(member)
.where(member.username.eq("member1")
.and(member.age.between(10, 30))
).fetchOne();
assertThat(findMember.getUsername()).isEqualTo("member1");
}
AND 조건을 파라미터로 처리
@Test
public void searchAndParam() {
Member findMember = queryFactory
.selectFrom(member)
.where(member.username.eq("member1")
,(member.age.eq(10))
).fetchOne();
assertThat(findMember.getUsername()).isEqualTo("member1");
}
- 콤마 ( , ) 인 경우 AND로 조립된다.
- 중간에 null이 들어갈 경우 무시한다. (동적쿼리 짤 때 좋다!)
'Spring관련 기술 > JPA' 카테고리의 다른 글
Querydsl - 정렬 (0) | 2024.01.16 |
---|---|
Querydsl - 결과 조회 (0) | 2024.01.16 |
Querydsl - 기본 QType 활용 (0) | 2024.01.16 |
JPQL vs Querydsl (0) | 2024.01.16 |
QueryDsl 라이브러리 살펴보기 (0) | 2024.01.14 |