2 回答

TA貢獻1993條經(jīng)驗 獲得超6個贊
好吧,如果使用Spring Boot,你可以使用存儲庫:
@Repository
public interface ConsumptionRepo extends JpaRepository<Consumption, Long>{
List<Consumption> findByUser(User user, Pageable pageable);
}
然后你可以簡單地調(diào)用它
ConsumptionRepo.findByUser(user, PageRequest.of(page, size);

TA貢獻1869條經(jīng)驗 獲得超4個贊
由于@mtshaikh想法,我終于為我的問題找到了解決方案:
只需實現(xiàn)一個分頁服務(wù):
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());
}
添加回答
舉報