return new ModelAndView("/user_list")
.addObject("id", id) // attribute명: id, 값: id값
.addObject("name", name);
하기 코드를 통해 tymeleaf 로도 받을 수 있음
${id}, ${name}
<div th:replace="fragment/profile :: profile(${id}, ${name})"></div>
@PageableDefault(sort={"id"}, direction= Sort.Direction.DESC) Pageable pageable
- PageableDefault를 이용해 id값을 내림차순으로 정렬한 값을 pageable에 삽입
@GetMapping("") // http://localhost:8888/api/user?page=1
public Header<List<UserApiResponse>> findAll(@PageableDefault(size=10, sort={"id"}, direction= Sort.Direction.DESC) Pageable pageable){
return userApiLogicService.search(pageable);
}
Pageable
JPA에서 제공하는 서비스
Collectors
객체들을 자동으로 담아줌
collect(Collectors.toList() // 객체들을 List형으로 담음
public Header<List<UserApiResponse>> search(Pageable pageable){
Page<Users> users = baseRepository.findAll(pageable);
List<UserApiResponse> userApiResponses = users.stream().map(
user -> response(user)).collect(Collectors.toList());
Pagination pagination = Pagination.builder()
.totalPages(users.getTotalPages())
.totalElements(users.getTotalElements())
.currentPage(users.getNumber())
.currentElements(users.getNumberOfElements())
.build();
return Header.OK(userApiResponses, pagination);
}