QueryDSL

검색 조건 쿼리(feat. QueryDSL)

쿠카이든 2022. 6. 17. 12:53
728x90
JPAQuery query = new JPAQuery(em);  //검색 조건 쿼리 실행
QItem item = QItem.item;
List<Item> list = query.from(item)
    .where(item.name.eq("좋은상품").and(item.price.gt(20000)))
    .list(item);  //조회할 프로젝션 지정
select item    --실행된 JPQL
  from Item item
 where item.name ?1 and item.price > ?2
  • QueryDSL의 where 절에는 and 나 or을 사용할 수 있다. 또한 다음처럼 여러 검색 조건을 사용해도 된다. 이때는 and 연산이 된다.
.where(item.name.eq("좋은 상품"), item.price.gt(20000))
  • 쿼리 타입의 필드에는 필요한 대부분의 메소드를 명시적으로 제공한다.
  • 다음은 where() 에서 사용되는 메소드다.
item.price.between(10000, 20000);  //가격이 10000원 ~ 20000원 상품
item.name.contains("상품");        //상품1이라는 이름을 포함한 상품,
                                   //SQL에서 like '%상품%' 검색
item.name.startsWith("고급");      //이름이 고급으로 시작하는 상품,
                                   //SQL에서 like '고급%' 검색

 

출처 : JAVA 표준 ORM JPA 프로그래밍 (저자 김영한)

728x90

'QueryDSL' 카테고리의 다른 글

(QueryDSL) 동적쿼리(feat. BooleanBuilder)  (0) 2022.06.18
(JPA)프로젝션 feat.QueryDSL..  (0) 2022.06.18
QueryDsl 조인 종류  (0) 2022.06.17
페이징과 정렬(feat.queryDSL)  (0) 2022.06.17
기본 Q 생성  (0) 2022.06.17