簡體   English   中英

分頁和規范用法 Spring 數據

[英]Pagination & Specification usage Spring Data

我正在嘗試為我的交易添加分頁

下面的解決方案工作得很好

List<AccountTransactions> products = accountTransactionRepository.findAll(specification);

int skip = (page - 1) * ps;

List<AccountTransactionDto> transactionDtos =
        AccountTransactionMapper.toAccountTransactionDtoList(products).stream()
                .skip(skip).limit(ps).collect(Collectors.toList());

但我試圖通過使用 spring 數據作為關於第一篇文章中的建議。 另一個原因是我認為 usign stream 會導致性能問題。

存儲庫 class

@Repository
public interface AccountTransactionRepository extends PagingAndSortingRepository<AccountTransactions, Integer>,
        JpaSpecificationExecutor<AccountTransactions> {
}

服務實現。 class

Pageable pageable = PageRequest.of(1, 5);

Page<List<AccountTransactions>> products = accountTransactionRepository.findAll(specification,pageable);

但這會返回一個錯誤

Required type:
List
<AccountTransactions>
Provided:
Page
<AccountTransactions>

我想我需要返回 Page<List> 作為返回類型。

當我將其轉換為 Page 時,它可以工作但返回錯誤的結果。

另外,您是否知道從性能角度來看最好的方法是什么?

findAll默認返回 Page 實例。 因此,您必須更正以下行:

Page<List<AccountTransactions>> products = accountTransactionRepository.findAll(specification,pageable);

Page<AccountTransactions> products = accountTransactionRepository.findAll(pageable);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM