spring boot整合pageHelper分页插件

pageHelper是一个很好的分页插件,可以帮助我们方便快捷的完成数据分页操作,减轻工作量,而且简单易用。

1.导入依赖

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

2.配置分页插件

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

3.编写相应接口

    @Select("select * from student_info")
    List<Student> selectAll();

4.service层

调用分页 Page<Object> page = PageHelper.startPage(2,2);参数分别是(当前页码,每页记录数)

    //整合pageHelper分页插件
        public List<Student> selectAll(){
            //设置当前所在页面和每页数据数目,在调用查询方法前调用
            Page<Object> page = PageHelper.startPage(2,2);
            //page的相关方法
            System.out.println("每页记录数:"+page.getPageSize());
            System.out.println("总记录数:"+page.getTotal());
            System.out.println("当前页码:"+page.getPageNum());
            System.out.println("结果集:"+page.getResult());
            List<Student> list = studentDao.selectAll();
            //pageInfo封装页面信息
            PageInfo pageInfo = new PageInfo(list);
            System.out.println("结果集:"+pageInfo.getList());
            System.out.println("当前页面:"+pageInfo.getPageNum());
            System.out.println("总页数:"+pageInfo.getPages());
            System.out.println("当前页面显示的数据条目:"+pageInfo.getPageSize());
            System.out.println("下一页:"+pageInfo.getNextPage());
            System.out.println("上一页:"+pageInfo.getPrePage());
            System.out.println("是否有下一页:"+pageInfo.isHasNextPage());
            System.out.println("是否有上一页:"+pageInfo.isHasPreviousPage());
            return list;
        }

5.controller层

     @GetMapping("/selectAll")
        public Object selectAll(){
            return studentService.selectAll();
        }

6.测试

在这里插入图片描述
在这里插入图片描述

7.扩展

还可以利用pageInfo向前台传输数据

     @GetMapping("/selectAll")
        public String selectAll(Model model){
            List<Student> list = studentService.selectAll();
            PageInfo pageInfo = new PageInfo(list);
            //利用pageInfo封装的信息,向前台传输数据
            model.addAttribute("pageinfo",pageInfo);
            return "list";
        }

前台list页面

    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <div align="center">
        <table border="1">
            <tr>
                <th>id</th>
                <th>name</th>
                <th>sex</th>
                <th>age</th>
            </tr>
            <tr th:each="person:${pageInfo.list}">
                <td th:text="${person.id}"></td>
                <td th:text="${person.name}"></td>
                <td th:text="${person.sex}"></td>
                <td th:text="${person.age}"></td>
            </tr>
        </table>
        <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>
    </div>
    </body>
    </html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值