-
페이징은 db 조회 결과를 특정한 갯수로 모아서 반환해준다.
public interface PlaceRepository extends JpaRepository<Place, Long>,placeRepositoryCustom { Page<Place> findAllByCategoryAndRegion1AndRegion2In(Pageable pageable,String category, String region1, String[] region2); Page<Place> findAllByCategoryAndRegion1(Pageable pageable,String category, String regio n1); }
스프링 데이터 JPA 에서는 페이징을 쉽게 구현하도록 인터페이스를 제공해준다.
JpaRepository를 해당 리포지토리에 상속되도록 하면 List 대신 Page 를 사용하여 페이징을 제공한다.
요청 url :
https://for-rest.herokuapp.com/places/list?page=1&size=12&category=박물관@region_1=서울@region_2=강서구
query string
- page=1 -> 2번째 페이지를 반환 ( page를 포함 안할시 첫번째 페이지 반환)
- size=12 -> 한 페이지에는 12개의 정보가 있음 (예시 코드에는 12개의 place) -> size를 포함 안할시 기본값 20
추가로, sort=id 를 쿼리스트링에 붙이면 id 값으로 정렬된 값들이 페이징되어 반환된다.
요청 url :
https://for-rest.herokuapp.com/places/list?size=12&category=박물관®ion_1=서울®ion_2=강서구
반환 결과 :
{ "content": [ { "id": 6783, "name": "국립항공박물관", "address": "서울 강서구 하늘길 177", "img_src": "https://search.pstatic.net/common/?autoRotate=true&type=w560_sharpen&src=https%3A%2F%2Fldb-phinf.pstatic.net%2F20201120_257%2F1605854850079eD8Vf_JPEG%2Fcg3nA9RA2gA9A1zjDInCZISY.jpg", "tag": "#박물관\n#항공박물관\n#국립박물관\n#교육\n#전시", "likeCount": 0 }, { "id": 6784, "name": "허준박물관", "address": "서울 강서구 허준로 87 허준박물관", "img_src": "https://search.pstatic.net/common/?autoRotate=true&type=w560_sharpen&src=https%3A%2F%2Fldb-phinf.pstatic.net%2F20220118_52%2F16424910816151WXzj_JPEG%2F%25C7%25E3%25C1%25D8%25B9%25DA%25B9%25B0%25B0%25FC%25C0%25FC%25B0%25E6-%25B1%25DB%25C0%25DA-%25B3%25AA%25B9%25AB%25C3%25DF%25B0%25A1.jpg_copy.jpg", "tag": null, "likeCount": 0 }, { "id": 6785, "name": "충우곤충박물관", "address": "서울 강서구 강서로 139 충우곤충박물관", "img_src": "https://search.pstatic.net/common/?autoRotate=true&type=w560_sharpen&src=https%3A%2F%2Fldb-phinf.pstatic.net%2F20150831_94%2F1441011957065nI112_JPEG%2FSUBMIT_1327456641658_11655764.jpg", "tag": "#사슴벌레\n#장수풍뎅이\n#애완곤충\n#곤충\n#박물관", "likeCount": 0 }, { "id": 6786, "name": "기생충박물관", "address": "서울 강서구 화곡로 333", "img_src": "https://search.pstatic.net/common/?autoRotate=true&type=w560_sharpen&src=https%3A%2F%2Fldb-phinf.pstatic.net%2F20211027_291%2F1635325279623JxEGi_JPEG%2FXITSww8Wfmp1yoz83kwYbeRE.jpg", "tag": "#박물관\n#기생충\n#전문박물관\n#서울시박물관\n#강서구박물관", "likeCount": 0 } ], "pageable": { "sort": { "sorted": false, "unsorted": true, "empty": true }, "pageNumber": 0, "pageSize": 12, "offset": 0, "unpaged": false, "paged": true }, "last": true, "totalPages": 1, "totalElements": 4, "first": true, "sort": { "sorted": false, "unsorted": true, "empty": true }, "number": 0, "numberOfElements": 4, "size": 12, "empty": false }
'Spring' 카테고리의 다른 글
에러핸들링 : GlobalExceptionHandler (0) 2022.04.28 스프링부트 2.6 과 Swagger-ui 가 호환 안되는 문제 (0) 2022.04.26 API 명세서 (0) 2022.04.19 ERD 설계 (0) 2022.04.19 협업 프로젝트 시작 (0) 2022.03.22