上一篇博客記錄了Jpa環境的搭建,有興趣的童鞋可以去看看:https://blog.csdn.net/jsnhux/article/details/95493350
注:本篇博客的實例是接上一篇博客的,有些代碼不會詳細再寫一遍,只保留功能點所需代碼儘量做到精簡可用
1、分頁功能
UserRepository
@Query("select u from User u")
Page<User> findALL(Pageable pageable);
測試
@Test
public void test1() {
//0:表示頁數(第一頁是0);2:每頁顯示條數 ;Sort.Direction.DESC,"id"):根據id降序排序
Pageable pageable = new PageRequest(0,2, Sort.Direction.DESC,"id");
Page<User> page = userRepository.findALL(pageable);
//查詢結果總行數
System.out.println(page.getTotalElements());
//查詢按照當前分頁大小,總頁數
System.out.println(page.getTotalPages());
//查詢當前分頁條件返回的數據集合
List<User> content = page.getContent();
for (User u : content) {
System.out.println("u="+u);
}
}