上一篇博客记录了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);
}
}