SpringBoot分页插件的实现过程

实现分页插件的具体步骤:

1.导入jar包


		<dependency>
			<groupId>com.github.pagehelpergroupId>
			<artifactId>pagehelper-spring-boot-starterartifactId>
			<version>1.2.5version>
		dependency>


		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-testartifactId>
			<scope>testscope>
		dependency>

2.(两种)在application.yml(propreties)文件加上
第一种:yml

pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countSql

第二种:propreties

pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql

3.在controller控制层编写代码

  @GetMapping("/getAllPerson")
    public String getAllPerson(Model model,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,
                               String singleName){
                               
        PageHelper.startPage(pageNum,2); //设置当前页码,每页的行数

        List<Tabulation> list = aaService.getAllPerson(singleName); //查询的集合
        PageInfo<Tabulation> pageInfo = new PageInfo<Tabulation>(list); //把查询的集合放入pageInfo返回给页面
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("name",singleName);
        return "index";
    }

4.编写页面

 <p>当前 <span th:text="${pageInfo.pageNum}">span> 页,总 <span th:text="${pageInfo.pages}">span> 页,共 <span th:text="${pageInfo.total}">span> 条记录p>
    <a th:href="@{/getAllPerson}">首页a>
    <a th:href="@{/getAllPerson(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}">上一页a>
    <a th:href="@{/getAllPerson(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一页a>
    <a th:href="@{/getAllPerson(pageNum=${pageInfo.pages})}">尾页a>

5.运行
SpringBoot分页插件的实现过程_第1张图片
SpringBoot分页插件的实现过程_第2张图片

总结

使用分页插件无需创建page类,也不用在查询语句中写limit

你可能感兴趣的:(分页插件,spring,boot)