Q는 엔티티마다 자동으로 생성해 준다.
Gradle -> tasks -> other -> compileQuerydsl 실행
하면 build -> generated -> querydsl -> 팩토리에 Q가 생성된다
JPAQueryFactory jpaQueryFactory = new JPAQueryFactory(em);
//필드
JPAQueryFactory jpaQueryFactory;
@BeforeEach
public void before(){
jpaQueryFactory = new JPAQueryFactory(em);
}
QMember qMember = QMember.member;
Querydsl은 JPQL 빌더
: querydsl은 바인딩을 직업 set으로 넣어주지 않아도 체이닝하여 자동으로 프리페어 스테이트먼트 파라미터 바인딩 형식으로 사용한다
@DisplayName("JPQL 쿼리 확인.")
@Test
void startJPQL() {
//member1을 조회
Member findByJPQL = em.createQuery("select m from Member m where m.username = :username", Member.class)
.setParameter("username", "member1")
.getSingleResult();
assertThat(findByJPQL.getUsername()).isEqualTo("member1");
}
@DisplayName("Querydsl 쿼리 확인.")
@Test
void StartQuerydsl() {
JPAQueryFactory jpaQueryFactory = new JPAQueryFactory(em);
QMember qMember = QMember.member;
Member resultFindMember = jpaQueryFactory
.selectFrom(qMember)
.from(qMember)
.where(qMember.username.eq("member1"))
.fetchOne();
assertThat(resultFindMember.getUsername()).isEqualTo("member1");
}