[英]Spring Data Pagination returns wrong number of content as it is requested
目前,我正在開發一個使用Spring Data Pagination
的api,並且遇到一個問題,即我將一個Pageable
對象作為請求傳遞給我的存儲庫,我希望使用該頁面接收哪個頁面以及應該包含多少個元素。 我的對象如下所示: Pageable pageable = PageRequest.of(0, 2);
-所以我要第一頁包含兩個元素。 在我的數據庫中有3個對象,因此將有2頁。 但是,我的想法是: 屏幕截圖 。 誰能告訴我為什么它表明內容是3個元素的數組,但實際上我要2個元素?
@Override
public List<NotificationDto> getLast30NotificationsFor(ApplicationUser user) {
Pageable pageable = PageRequest.of(0, 2);
Page<Notification> first30ByOwnerIdOrderByCreatedAtDesc = notificationRepository.findFirst30ByOwnerIdOrderByCreatedAtDesc(user.getId(), pageable);
List<Notification> notifications = new ArrayList<>(first30ByOwnerIdOrderByCreatedAtDesc.getContent());
return notifications.stream()
.map(NotificationToDtoMapper::map)
.collect(toList());
}
嘗試:
@Override
public List<NotificationDto> getLast30NotificationsFor(ApplicationUser user) {
Pageable page = PageRequest.of(0,2, Sort.by("createdAt").descending());
return notificationRepository.findByOwnerId(user.getId(), page)
.getContent()
.stream()
.map(NotificationToDtoMapper::map)
.collect(toList());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.