springboot_使用Pagehelper插件实现分页

原文地址:https://blog.csdn.net/qq_27317475/article/details/81168241

一、pom文件中引入Pagehelper依赖

<!-- 分页插件 -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
</dependency>

二、配置分页插件

打开application.properties,添加如下几行配置信息

#分页插件
pagehelper.helper-dialect=mysql
pagehelper.params=count=countSql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true

三、在需要读取的表里先添加多条数据

我这里在我的studet表中添加了10多条的数据

四、修改StudentController内容

    @RequestMapping("/index")
    public String index(Model model,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum,2);
        List<Student> studentList = studentService.selectAllStudent();
        PageInfo<Student> pageInfo = new PageInfo<Student>(studentList);
        model.addAttribute("pageInfo", pageInfo);
        return "index";
    }

其实使用PageHelper配置好之后Controller只需要在查询语句之前写PageHelper.startPage(pageNum,2),PageHelper.startPage(int PageNum,int PageSize):用来设置页面的位置和展示的数据条目数,我们设置每页展示2条数据在查询语句之后写上PageInfo<Student> pageInfo = new PageInfo<Student>(studentList)

  • PageInfo.list    结果集
  • PageInfo.pageNum    当前页码
  • PageInfo.pageSize    当前页面显示的数据条目
  • PageInfo.pages    总页数
  • PageInfo.total    数据的总条目数
  • PageInfo.prePage    上一页
  • PageInfo.nextPage    下一页
  • PageInfo.isFirstPage    是否为第一页
  • PageInfo.isLastPage    是否为最后一页
  • PageInfo.hasPreviousPage    是否有上一页
  • PageHelper.hasNextPage    是否有下一页
     

五、处理返回到list界面的数据信息,依然使用thymeleaf模版

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/> 
    <title>Title</title>
</head>

<body style="'margin:50px;'">
<table class="table table-striped">
    <thead>
    <tr>
        <th>序号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>分数</th>
    </tr>
    </thead>

    <tbody>
    <tr th:each="stu:${pageInfo.getList()}">
        <td th:text="${stuStat.count}">1</td>
        <td th:text="${stu.name}">jack</td>
        <td th:text="${stu.age}">11</td>
        <td th:text="${stu.score}">98</td>
        <td>
            <a th:href="@{/student/toUpdate(id=${stu.id})}">修改</a>
            <a th:href="@{/student/delete(id=${stu.id})}">删除</a>
        </td>
    </tr>
    </tbody>
</table>
<p>当前 <span th:text="${pageInfo.pageNum}"></span> 页,总 <span th:text="${pageInfo.pages}"></span> 页,共 <span th:text="${pageInfo.total}"></span> 条记录</p>
<a th:href="@{/index}">首页</a>
<a th:href="@{/index(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}">上一页</a>
<a th:href="@{/index(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一页</a>
<a th:href="@{/index(pageNum=${pageInfo.pages})}">尾页</a>
</body>
</html>

六、显示结果

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值