Spring Boot 整合Pagehelper分页插件

1、在pom文件中引入Pagehelper分页插件,编辑pom.xml

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

2、配置分页插件,编辑application.properties,添加如下配置

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

3、修改controller代码

    @GetMapping("")
    public ReturnResult index() {
        PageHelper.startPage(1, 1);

        List<UserBean> list = userService.getUsers();

        PageInfo<UserBean> userBeanPageInfo = new PageInfo<>(list);

        return ReturnResult.data(userBeanPageInfo, "获取用户列表成功");
    }

其中:PageHelper.startPage(int PageNum,int PageSize):用来设置页面的位置和展示的数据条目数,我们设置每页展示5条数据。PageInfo用来封装页面信息,返回给前台界面。PageInfo中的一些我们需要用到的参数如下表:

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

注意:mapper.xml文件中的sql语句最后不要添加;,否则会报错。

如果查看sql时发现只有一个参数,可能是因为pageNum传入了1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值