스프링 MVC 의 Handler(=Controller) 의 매개변수를 확장시킨다.
- Pageable
- Sort
@GetMapping("/posts")
public Page<Post> getPosts(Pageable pageable){
return postRepository.findAll(pageable);
}
Pageable 을 위한 파라미터
- page : 0 부터 시작, 페이지 넘버
- size : 가져올 레코드 수, default 20
- sort : 프로퍼티,(ASC|DESC) default asc
- 쿼리스트링은 이런식 : sort=property,desc&sort=property
@Test
public void testPosts() throws Exception {
Post post = new Post();
post.setTitle("JPA");
postRepository.save(post);
mockMvc.perform(MockMvcRequestBuilders.get("/posts")
.param("page", "0")
.param("size", "10")
.param("sort","created,desc")
.param("sort", "title")) // created 가 동일할 경우 정렬기준
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.content[0].title", is("JPA")))
.andDo(MockMvcResultHandlers.print());
}
Hibernate:
select
post0_.id as id1_0_,
post0_.created as created2_0_,
post0_.title as title3_0_
from
post post0_
order by
post0_.created desc,
post0_.title asc limit ?
///
Body = {"content":[{"id":1,"title":"JPA","created":null}],
"pageable":{"sort":{"sorted":true,"unsorted":false,"empty":false},
"offset":0,"pageSize":10,"pageNumber":0,"paged":true,"unpaged":false},
"last":true,"totalPages":1,"totalElements":1,"number":0,"size":10,"sort":{"sorted":true,"unsorted":false,"empty":false},
"numberOfElements":1,"first":true,"empty":false}
'springframework > Spring Data JPA' 카테고리의 다른 글
Spring Data JPA : JPA Repository (0) | 2020.11.18 |
---|---|
Spring Data Common : Web - HATEOAS (0) | 2020.11.17 |
Spring Data Common : Web - Domain Class Converter (0) | 2020.11.17 |
Spring Data Common : Web 소개 (0) | 2020.11.17 |
Spring Datat Common : QueryDSL 변경사항 (0) | 2020.11.17 |