ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Spring boot - Querydsl
    공부/Spring 2023. 11. 27. 21:44

    *Querydsl

     

    *장점

    - JPA의 Query문에 오타가 프로그램이 실행되나 메소드 사용시 문제가 생기게 된다

    Querydsl은 클래스에 있는 이름을 사용하기 때문에 코드상에서 문제를 발견 할 수있다

    - JPA를 코드적으로 활용하서 사용할 수 있다

     

    *JPAQuery 데이터 반환 메소드

    메소드 기능
    List<T> fetch() 조회 결과 리스트 반환
    T fatchOne 조회 대상이 1건인 경우 제네릭으로 지정한 타입 반환
    T fatchFirst() 조회 대상 중 1건만 반환
    Long fetchCount() 조회 대상 개수 반환
    QueryResult<T> fetchResults() 조회한 리스트와 젠체 개수를 포함한 QueryResults 반환

     

    *QuerydslPredicateExecutor

    메소드 기능
    long count(Predicate) 조건에 맞는 데이터의 총 개수 반환
    boolean exists(Predicate) 조건에 맞는 데이터 존재 여부 반환
    Iterable findAll(Predicate) 조건에 맞는 모든 데이터 반환
    Page<T> findAll(Predicate, pageable) 조건에 맞는 페이지 데이터 반환
    Iterable findAll(Predicate, Sort) 조건에 맞는 정렬된 디에터 반환
    T findOne(Predicate) 조건에 맞는 데이터 1개 반환

     

    @Test
        @DisplayName("Querydsl 조회 테스트1")
        public void queryDslTest(){
            this.createItemList();
            JPAQueryFactory queryFactory = new JPAQueryFactory(em);
            QItem qItem = QItem.item;
            JPAQuery<Item> query  = queryFactory.selectFrom(qItem)
                    .where(qItem.itemSellStatus.eq(ItemSellStatus.SELL))
                    .where(qItem.itemDetail.like("%" + "테스트 상품 상세 설명" + "%"))
                    .orderBy(qItem.price.desc());
    
            List<Item> itemList = query.fetch();
    
            for(Item item : itemList){
                System.out.println(item.toString());
            }
        }

    '공부 > Spring' 카테고리의 다른 글

    Spring boot - 연관 관계 매핑  (0) 2023.12.06
    Spring boot - 권한  (0) 2023.12.05
    Spring boot - JPA - 쿼리 메소드, JPQL snippet  (0) 2023.11.11
    Spring boot - Entity  (0) 2023.11.11
    Spring boot - JPA - MySQL 연결 설정  (0) 2023.11.11

    댓글

Designed by Tistory.