java - Spring Data Pagination 在请求时返回错误数量的内容
问题描述
目前我正在开发一个使用的api,Spring Data Pagination
我遇到了一个问题,我将一个Pageable
对象作为请求传递给我的存储库,我想要接收哪个页面以及应该有多少元素。有了这个我的对象看起来像:Pageable pageable = PageRequest.of(0, 2);
- 所以我想要有两个元素的第一页。在我的数据库中有 3 个对象,因此将有 2 页。但是 ide 显示给我的是:screenshot。谁能告诉我为什么它显示内容是一个由 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());
}
推荐阅读
- delphi - 数据模块上的 Delphi THTTPRIO 组件导致 EInvalidPointer
- php - 如何使用 PHP 和 Gmail API 从我的个人 gmail 帐户发送电子邮件?
- wordpress - 如何在 laravel 项目上启用 SSO 并将其与 WHMCS 和 WordPress 集成
- python - 导入多张图片
- c++ - OpenGL - 未渲染的顶点/三角形
- azure-logic-apps - 使用 Azure 逻辑应用跨 Azure DevOps 项目触发“创建的工作项”
- python - 在python中避免小数部分
- c# - 我应该一次触发多少个 IO 绑定异步任务?
- node.js - 如何在 Linux Docker 容器上安装 NPM
- javascript - 如何获取 JSON 键的路径:值对