(2/2) Item Entity, Repository, Test 실습 Querydsl, paging > 스프링 부트

본문 바로가기

[개발] (2/2) Item Entity, Repository, Test 실습 Querydsl, paging

필기자
2022-09-13 16:34 6,509 0

본문

Item Entity, Repository, Test 실습 Querydsl, paging

 

kr/hull/shop/repository/ItemRepository.java 다음 인터페이스 추가 상속



public interface ItemRepository extends JpaRepository<Item, Long>, QuerydslPredicateExecutor<Item> {
    .
    .
    .
}

 

src/test/java/kr/hull/shop/repository/ItemRepositoryTest.java 다음 내용 추가(이어 쓰기)



    public void createItemListPaging(){
        for(int i=1;i<=5;i++){
            Item item = new Item();
            item.setItemNm("좋은 상품" + i);
            item.setPrice(10000 + i);
            item.setItemDetail("좋은 상품 설명" + i);
            item.setItemSellStatus(ItemSellStatus.SELL);
            item.setStockNumber(100);
            item.setRegTime(LocalDateTime.now());
            item.setUpdateTime(LocalDateTime.now());
            itemRepository.save(item);
        }

        for(int i=6;i<=10;i++){
            Item item = new Item();
            item.setItemNm("좋은 상품" + i);
            item.setPrice(10000 + i);
            item.setItemDetail("좋은 상품 설명" + i);
            item.setItemSellStatus(ItemSellStatus.SOLD_OUT);
            item.setStockNumber(0);
            item.setRegTime(LocalDateTime.now());
            item.setUpdateTime(LocalDateTime.now());
            itemRepository.save(item);
        }
    }

    @Test
    @DisplayName("상품 Querydsl 페이징 조회 확인")
    public void queryDslPagingTest(){

        this.createItemListPaging();

        BooleanBuilder booleanBuilder = new BooleanBuilder();
        QItem item = QItem.item;

        String itemDetail = "좋은 상품 설명";
        int price = 10003;
        String itemSellStat = "SELL";

        booleanBuilder.and(item.itemDetail.like("%" + itemDetail + "%"));
        booleanBuilder.and(item.price.gt(price));
        System.out.println(ItemSellStatus.SELL);

        if(StringUtils.equals(itemSellStat, ItemSellStatus.SELL)){
            booleanBuilder.and(item.itemSellStatus.eq(ItemSellStatus.SELL));
        }

        Pageable pageable = PageRequest.of(0, 5);
        Page<Item> itemPagingResult = itemRepository.findAll(booleanBuilder, pageable);
        System.out.println("total elements : " + itemPagingResult. getTotalElements ());

        List<Item> resultItemList = itemPagingResult.getContent();
        for(Item resultItem: resultItemList){
            System.out.println(resultItem.toString());
        }
    }

 

입력 데이터 확인

 

20220913163421_ddfa29dc4cb6a70c618839d7aa582e62_h34t.png

 

결과 QUERY



    select
        item0_.item_id as item_id1_0_,
        item0_.item_detail as item_det2_0_,
        item0_.item_nm as item_nm3_0_,
        item0_.item_sell_status as item_sel4_0_,
        item0_.price as price5_0_,
        item0_.reg_time as reg_time6_0_,
        item0_.stock_number as stock_nu7_0_,
        item0_.update_time as update_t8_0_ 
    from
        item item0_ 
    where
        (
            item0_.item_detail like '%좋은 상품 설명%' escape '!'
        ) 
        and item0_.price>10003 
        and item0_.item_sell_status='SELL' limit 5

 

결과

 

20220913163244_ddfa29dc4cb6a70c618839d7aa582e62_cw3f.png20220913163318_ddfa29dc4cb6a70c618839d7aa582e62_780c.png

댓글목록0

등록된 댓글이 없습니다.
게시판 전체검색