QueryDSL - 검색 조건 쿼리

목차

@Test
public void startQuerydsl(){
QMember qMember = new QMember("m");

Member findMember = queryFactory
.select(qMember)
.from(qMember)
.where(qMember.username.eq("member1"))
.fetchOne();

assertThat(findMember.getUsername()).isEqualTo("member1");
}
@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");
}

and 를 풀어내는 방식 - and 메소드 사용

@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");
}

and 를 풀어내는 방식 - 파라미터로 넘기는 방법

@Test
public void search(){
Member findMember = queryFactory
.selectFrom(member)
.where(
member.username.eq("member1"),
member.age.eq(10)
)
.fetchOne();

assertThat(findMember.getUsername()).isEqualTo("member1");
}

Querydsl 에서 사용하는 검색 조건

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%’ 검색
Share