
Spring-Data-JPA 中的分頁
問題:
問題:
在什麼>
答案:
從Spring Data JPA 1.7.0 開始:
1 | <code class = "java" >findTop10ByLastnameOrderByFirstnameAsc(String lastname);</code>
|
登入後複製
Top 和First 開始:
Top 和First 關鍵字中進行分頁:
Spring Data 將相應地限制結果。
1 2 3 4 5 6 | <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>
|
登入後複製
對於先前的版本:
1 2 3 4 5 6 | <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中文網其他相關文章!