繁体   English   中英

Spring Data Pagination返回错误的内容数量

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM