스터디/Spring

[Spring Boot] 정렬과 페이징

menuhwang 2022. 10. 9. 21:43

정렬과 페이징

정렬


레코드 조회 시 가격 혹은 등록 날짜 등 특정 컬럼을 기준으로 정렬이 필요한 경우가 있다.

쿼리 메서드를 활용하는 방법과 정렬을 위한 객체를 인자로 전달하는 방법이 있다.

쿼리 메서드를 활용하는 방법

List<Product> findByNameOrderByPriceAsc(String name);

쿼리 메서드를 활용하는 방법은 메서드 이름이 매우 길어진다는 문제가 있다.

정렬을 위한 Sort객체를 인자로 전달하는 방법을 사용하면 메서드 이름을 짧게 작성할 수 있다.

파라미터로 Sort정보를 받아오는 방법

List<Product> products = productRepository.findByNameContains("네스프레소", Sort.by(Sort.Order.asc("name"), Sort.Order.asc("price")));
        /*
        Hibernate:
            select
                product0_.id as id1_0_,
                product0_.category as category2_0_,
                product0_.name as name3_0_,
                product0_.price as price4_0_
            from
                product product0_
            where
                product0_.name like ? escape ?
            order by
                product0_.name asc,
                product0_.price asc
        */

위와 같이 여러 정렬 조건을 전달하는 것도 가능하다.

 

페이징


데이터를 n개씩 여러 페이지로 나눠 전달하는 것을 페이징이라고 한다.

페이징하는 방법은 간단하게 쿼리 메서드 인자에 Pageable 객체를 전달하면 된다.

Page<Product> products = productRepository.findAll(PageRequest.of(1, 5));
        /*
        Hibernate:
            select
                product0_.id as id1_0_,
                product0_.category as category2_0_,
                product0_.name as name3_0_,
                product0_.price as price4_0_
            from
                product product0_ limit ? offset ?
        Hibernate:
            select
                count(product0_.id) as col_0_0_
            from
                product product0_
        */
System.out.println(products.getContent()); // 페이지 객체에 담긴 데이터를 가져오는 메서드.
  • PageRequest.of(int page, int size)
  • PageRequest.of(int page, int size, Sort sort)

첫 번째 page는 가져올 페이지를 입력해주고, 두 번째 size에는 한 페이지에 몇 개의 레코드씩 가져올지 입력해주면 된다.

그리고 정렬이 필요하면 세 번째 인자에 Sort객체를 전달해 정렬된 레코드를 페이징하여 가져올 수 있다.