根據 Spring 檔案
但是,當我使用頁面2(或更高版本)呼叫存盤庫時,它突然在選擇查詢之后執行計數查詢,這是我沒想到的。

有什么方法可以使計數查詢不是使用 Pageable發出的嗎?
我所做的存盤庫和服務代碼。
@Repository
public interface ArticleRepository extends CrudRepository<Article, Long> {
// Slice was also tested and did issue count query from page 2
List<Article> findAll(Pageable pageable);
}
@Service
public class ArticleService {
private final ArticleRepository articleRepository;
@Autowired
public ArticleService(
ArticleRepository articleRepository) {
this.articleRepository = articleRepository;
}
public List<Article> getArticles(int page) {
PageRequest pageRequest = PageRequest.of(page, 50, Sort.by("createdAt").descending());
return articleRepository.findAll(pageRequest);
}
}
uj5u.com熱心網友回復:
如果您需要擴展 CrudRepository,那么您將能夠在您的存盤庫中使用Slice定義方法:
Slice<Article> findAll(Pageable pageable);
我認為另一個選項是將超級界面更改為PagingAndSortingRepository. 它可以解決使用List上的回傳。
interface ArticleRepository extends PagingAndSortingRepository<Article, Long> {
List<Article> findAll(Pageable pageable);
}
uj5u.com熱心網友回復:
- 您可以組合 PagingAndSortingRepository 和 CrudRepository 因為它們都是介面或使用 JpaRepository 代替。
- 您可以通過將其設定為 @Query 注釋來撰寫自己的計數查詢。
在源代碼中public Page<T> findAll(Pageable pageable)有totalSupplier執行計數查詢以獲取總數。
if (content.size() != 0 && pageable.getPageSize() > content.size()) {
return new PageImpl<>(content, pageable, pageable.getOffset() content.size());
}
return new PageImpl<>(content, pageable, totalSupplier.getAsLong());
所以你不能用默認的存盤庫方法來避免它。指定您自己的 jpa 方法或在PagingAndSortingRepository類下宣告查詢注釋。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/394441.html
上一篇:Vaadinui組件不兼容
