2 回答

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

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