2 回答
TA贡献1993条经验 获得超5个赞
好吧,如果使用Spring Boot,你可以使用存储库:
@Repository
public interface ConsumptionRepo extends JpaRepository<Consumption, Long>{
List<Consumption> findByUser(User user, Pageable pageable);
}
然后你可以简单地调用它
ConsumptionRepo.findByUser(user, PageRequest.of(page, size);
TA贡献1869条经验 获得超4个赞
由于@mtshaikh想法,我终于为我的问题找到了解决方案:
只需实现一个分页服务:
public Page<Consumption> getConsumptionListPaginated(Pageable pageable) {
int pageSize = pageable.getPageSize();
int currentPage = pageable.getPageNumber();
int startItem = currentPage * pageSize;
List<Consumption> list;
if (consumptionList.size() < startItem) {
list = Collections.emptyList();
} else {
int toIndex = Math.min(startItem + pageSize, consumptionList.size());
list = consumptionList.subList(startItem, toIndex);
}
return new PageImpl<>(list, PageRequest.of(currentPage, pageSize), consumptionList.size());
}
添加回答
举报