分页查询方法

spring提供了page,pageable接口来帮助实现分页功能的实现。

在原先查询中,Repository层不需要定义findAll()方法,可以直接在service层对其进行调用。但如果需要查询全部数据具有分页功能,需要在Repository层进行重写。

Repository层:

//Repository贮藏库,完成sql数据库操作
public interface BookRepository extends JpaRepository<book,Long> {Page<book> findAll(Pageable pageable);
}

Service层

@Service
public class BookService {@Autowiredprivate BookRepository bookRepository;//查询所有的书单信息————————原本的public List<book> findAll(){return bookRepository.findAll();}//分页查询所有的书单信息————————分页的public Page<book> findAllByPage(Pageable pageable){return bookRepository.findAll(pageable);}
}

Controller层

初始版:

@Controller
public class BookController {@Autowiredprivate BookService bookService;@GetMapping("/books")public String getAll(@RequestParam(defaultValue = "0") int page,@RequestParam(defaultValue = "5") int size,Model model){Page<book> bookList=bookService.findAllByPage(PageRequest.of(page,size));model.addAttribute("page",bookList);return "bookList";//PageRequest.of(page,size)会自动与pageable中属性对应。//PageRequest是Pageable的实现类。用于构造分页查询需要的页码(从0开始)、每页行数、排序等.    }
}

自动绑定page参数版本(建议使用)

@Controller
public class BookController {@Autowiredprivate BookService bookService;/*获取书单列表详情*/@RequestMapping("/books")public String list(@PageableDefault(size=5,sort={"id"},direction = Sort.Direction.DESC) Pageable pageable,Model model){//用@PageableDefault规定了pageable每页大小为5,按id排序,倒序排列。//不需要再使用pageRequest来装填pageable。Page<book> bookList=bookService.findAllByPage(pageable);model.addAttribute("page",bookList);return "bookList"; //指向templates中的books.html}
}

最后是bookList.jsp页面中翻页按钮的设置,用到了thymeleaf模板。

当且仅当page.first为真,即当前页是第一页,《上一页》按钮不会显示。

当且仅当page.last为真,即当前页是最后一页,《下一页》按钮不会显示。

<nav><ul class="pager"><li class="previous"><a href="#" th:href="@{'/books?page='+${page.number-1}}" th:unless="${page.first}">上一页</a></li><li class="next"><a href="#" th:href="@{/books(page=${page.number}+1)}" th:unless="${page.last}">下一页</a></li></ul>
</nav>

pageable分页处理相关推荐

  1. java mvc 分页查询条件_java分页条件查询-GridManager.js表格插件+Pageable分页对象+mybatis pagehelper分页插件...

    总览: 一. GridManager.js表格插件 直接上插件API:链接地址 感觉该插件简单好用,插件作者也是有问必答,nice 二. 添加依赖 后端: pom文件添加: 1.7.0.RELEASE ...

  2. jpa利用pageable分页排序

    jpa利用pageable分页排序 @RequestMapping(value = "/testPageable", method = RequestMethod.GET) pub ...

  3. pageable设置size_Spring的Pageable分页剖析

    PageRequest extends AbstractPageRequest 而 AbstractPageRequset implements Pageable first: 写一个接受分页参数的P ...

  4. SpringBoot实现分类搜索(模糊)查询 Pageable分页

    效果图如下: 数据库结构: Student学生表 stu表 sid学生id sname 学生姓名 sex性别 cid班级id address地址 sorts 排序 classes班级表 cid  cn ...

  5. EntityManager 使用 Pageable 分页

    参考:Springboot+JPA+Hibernate动态查询及分页,使用Pageable以及entityManager_扶我起来,我要学java的博客-CSDN博客_hibernate pageab ...

  6. 自定义java Pageable分页对象

    前两天写service层的方法中需要对数据库中的数据进行分页查询,本来都是在接口层由前端传过来一个Pageable对象,在接口中对Pageable对象用注解进行定义,所以一时间不知道怎么写,后来得知, ...

  7. pageable设置size_使用Pageable 分页

    1. service层 封装 packagecom.example.demo.service;importjava.util.List;importorg.springframework.data.d ...

  8. Pageable分页

    https://www.jianshu.com/p/67249c7b81d4 转载于:https://www.cnblogs.com/bbllw/p/10789139.html

  9. jpa分组分页查询 返回总数错误解决

    问题描述 jpa分组分页查询之后,返回page分页数据错误解决方案` 例如: Specification<User> specification = new Specification&l ...

最新文章

  1. 自动布局按钮排列平均分布
  2. javascript 队列
  3. 成功解决AttributeError: 'DataFrame' object has no attribute 'reshape'
  4. insert在python中的用法_python中insert用法是什么_后端开发
  5. 写一个函数取出php,写一个函数,尽可能高效的,从一个标准 url 里取出文件的扩展名...
  6. [No0000E0]批量打开当前路径下的文件
  7. Java网络通信 TCP网络,ServerSocket类
  8. 服务器与客户端的简单实现
  9. 清明上河图30亿像素_清明上河图高清下载|
  10. 2019111 控制台上实现极乐净土(有图有背景音乐)
  11. Eclipse安装漂亮的Darkest Dark Theme主题步骤(超详细)
  12. 教学实验平台之三极管放大特性测试
  13. 工商银行二维码业务开发
  14. 解决Android logcat: Unexpected EOF!方法指南
  15. 字写的不好没关系,还好我会python,轻轻一点就生成了艺术签名
  16. 联想win10专业版64位简体中文原版光盘镜像
  17. Windows7 tls加密协议设置导致的邮件收发问题 WLM 0x800CCC0B、0x800CCC0F
  18. 1688商品详情api接口
  19. Params和Body的区别
  20. vscode输入npm install报错

热门文章

  1. 2021-03-23(19. 删除链表的倒数第 N 个结点)
  2. 递推和递归的方法解决猴子吃桃问题(10天延伸到N天)——Java
  3. 自己写时间的Arduino时钟
  4. 一个木函v7.0.4 多功能工具箱
  5. vue阻止默认_vue中,阻止默认事件
  6. Kali渗透-NMAP高级使用技巧和漏洞扫描发现
  7. 百度地图android兼容,Android百度地图SDK无法支持64位平台完美解决方案
  8. linux 随机10字符病毒,Linux 10字符串命令病毒的处理记录
  9. 如何还原在HP Pavilion计算机出厂设置
  10. android oppo支付宝,安卓首家 OPPO联合支付宝开启3D人脸支付技术