首頁 > Java > java教程 > 主體

如何在 Spring Data JPA 中使用註解實作分頁?

Barbara Streisand
發布: 2024-10-29 03:04:29
原創
249 人瀏覽過

How Can I Implement Pagination in Spring Data JPA Using Annotations?

Spring-Data-JPA 中的分頁

問題:

問題:

在什麼>

答案:

從Spring Data JPA 1.7.0 開始:
<code class="java">findTop10ByLastnameOrderByFirstnameAsc(String lastname);</code>
登入後複製

Top 和First 開始:

Top 和First 關鍵字中進行分頁:

Spring Data 將相應地限制結果。
<code class="java">public interface UserRepository extends Repository<User, Long> {
  List<User> findByUsername(String username, Pageable pageable);
}

Pageable topTen = new PageRequest(0, 10);
List<User> result = repository.findByUsername("Matthews", topTen);</code>
登入後複製

對於先前的版本:
<code class="java">public interface UserRepository extends Repository<User, Long> {
  Page<User> findByUsername(String username, Pageable pageable);
}

Pageable topTen = new PageRequest(0, 10);
Page<User> result = repository.findByUsername("Matthews", topTen);</code>
登入後複製

使用Pageable介面和Page抽象完成分頁:如果需要上下文資訊,請使用Page作為傳回類型:使用Page需要觸發計數投影查詢來計算元資料。確保PageRequest包含排序資訊以獲得穩定的結果。

以上是如何在 Spring Data JPA 中使用註解實作分頁?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!