ページ実装
3955 ワード
せいぎょそうち
// 로그인한 회원이 등록한 상품들 조회
@GetMapping("/api/products")
public Page getProducts(
@RequestParam("page") int page,
@RequestParam("size") int size,
@RequestParam("sortBy") String sortBy,
@RequestParam("isAsc") boolean isAsc,
@AuthenticationPrincipal UserDetailsImpl userDetails
) {
Long userId = userDetails.getUser().getId();
page = page - 1;
return productService.getProducts(userId, page , size, sortBy, isAsc);
}
サービス
public Page getProducts(Long userId, int page, int size, String sortBy, boolean isAsc) {
Sort.Direction direction = isAsc ? Sort.Direction.ASC : Sort.Direction.DESC;
Sort sort = Sort.by(direction, sortBy);
Pageable pageable = PageRequest.of(page, size, sort);
return productRepository.findAllByUserId(userId, pageable);
}
きろく
public interface ProductRepository extends JpaRepository< Product, Long> {
Page findAllByUserId(Long userId, Pageable pageable);
}
Dtoの
@Transactional
public List getPagePlan1(int p){
int page = p - 1;
// Sort.Direction direction = Sort.Direction.DESC;
Sort sort = Sort.by(Sort.Direction.ASC, "createdAt");
Pageable pageable = PageRequest.of(page, 5, sort);
Page planPage = planRepository.findAll(pageable);
// 실험
Pageable pageable1 = planPage.getPageable();
long totalElements = planPage.getTotalElements();
boolean last = planPage.isLast();
int totalPages = planPage.getTotalPages();
int number = planPage.getNumber();
int size = planPage.getSize();
Sort sort1 = planPage.getSort();
int numberOfElements = planPage.getNumberOfElements();
boolean first = planPage.isFirst();
boolean empty = planPage.isEmpty();
List result = planPage.stream()
.map(plan -> new PlanAllResponseDto(plan,replyRepository.findAllByPlan(plan)))
.collect(Collectors.toList());
return result;
}
{"content": [
{
"createdAt": "2021-07-21T09:12:02.543",
"modifiedAt": "2021-07-21T09:12:02.543",
"planId": 1,
「title」:「テストタイトル1」、
[コンテンツ]:[コンテンツ1],
[planWriter]:[作者],
"planPassword": "1234",
"success": 0
},
{
"createdAt": "2021-07-21T09:12:02.757",
"modifiedAt": "2021-07-21T09:12:02.757",
"planId": 2,
「title」:「テストタイトル2」、
[コンテンツ]:[コンテンツ2],
[planWriter]:[作者],
"planPassword": "1234",
"success": 0
},
{
"createdAt": "2021-07-21T09:12:02.764",
"modifiedAt": "2021-07-21T09:12:02.764",
"planId": 3,
「title」:「テストタイトル3」,
[コンテンツ]:[コンテンツ2],
[planWriter]:[作者],
"planPassword": "1234",
"success": 0
},
],
"pageable": {
"sort": {
"sorted": true,
"unsorted": false,
"empty": false
},
"offset": 0,
"pageNumber": 0,
"pageSize": 5,
"unpaged": false,
"paged": true
},
"totalElements": 7,
"last": false,
"totalPages": 2,
"number": 0,
"size": 5,
"first": true,
"sort": {
"sorted": true,
"unsorted": false,
"empty": false
},
"numberOfElements": 5,
"empty": false
}
Reference
この問題について(ページ実装), 我々は、より多くの情報をここで見つけました https://velog.io/@cwangg897/페이지구현テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol