Springboot集成pagehelper 分页插件

引入依赖

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

application.yml配置

# PageHelper分页插件
pagehelper:
  helperDialect: mysql
  supportMethodsArguments: true
  params: count=countSql

controller

/**
 *  查询全部用户
 * @param pageNo        当前页
 * @param pageSize      条数
 * @param people
 * @return
 */
@GetMapping("/selectAll")
public List<People> selectAll( @RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "10") int pageSize,People people) {
    //接收前端传送的当前页和总条数,放入PageHelper当中,如果没有就取默认值 1和10
     PageHelper.startPage(pageNo,pageSize);
    return peopleService.selectAll(people);
}

结果:

日志打印

通过日志执行的sql语句我们可以看出,这里的10是取的默认值10,等于第一页,10条数据

模拟前端传入pageNo=1和pageSize  =2   表示第1页,2条数据

结果:

日志打印

通过执行的sql语句可以看出查询的条数为2条

坑:

这个坑叫做可能会出现,也可能不会出现,启动报错,叫做:  依赖循环

Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.

翻译:

不鼓励依赖循环引用,默认情况下禁止使用循环引用。更新应用程序以消除bean之间的依赖循环。作为最后手段,通过设置弹簧,可能会自动中断循环。主要的允许循环引用true。

依赖循环:

        详见:Spring面试之循环依赖(allowCircularReferences)_退休职工的博客-CSDN博客

导致原因:

我们打开maven库,找到 com---github---pagehelper---pagehelper-spring-boot-starter

找到对应的版本1.4.1,解压pagehelper-spring-boot-starter-1.4.1.jar包,

解压后打开META-INF文件,打开最里面的pom.xml,发现这个pom文件中引入了这些文件,发现与我们之前引入的依赖进行冲突了

解决方案:

1、修改yml配置

spring:
  main:
   allow-circular-references: true

2.解决冲突。修改spring的版本

将版本改为2.5.7,也可以正常启动

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot项目中集成PageHelper分页插件的步骤如下: 1. 打开Maven库,找到com.github.pagehelper:pagehelper-spring-boot-starter。 2. 确定需要使用的版本号,比如1.4.1。 3. 在项目的pom.xml文件中添加PageHelper的依赖。可以使用如下代码: ``` <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.4.1</version> </dependency> ``` 4. 在application.properties或application.yml配置文件中进行配置,添加以下属性: ``` # 开启分页支持 pagehelper.helper-dialect=mysql pagehelper.reasonable=true pagehelper.support-methods-arguments=true pagehelper.params=count=countSql ``` 这里的配置是针对MySQL数据库的,如果你使用的是其他数据库,需要根据对应的数据库类型进行配置。 5. 在需要分页的Mapper方法上使用PageHelper.startPage方法来开启分页功能。例如: ``` @Mapper public interface UserMapper { List<User> getUsers(); } ``` 在UserMapper接口中的方法上加上注解@Select,并在方法内部使用PageHelper.startPage方法开启分页,如下所示: ``` @Select("SELECT * FROM user") List<User> getUsers(); ``` 6. 运行项目,分页功能就会生效。 通过以上步骤,你就可以在Spring Boot项目中成功集成PageHelper分页插件了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [springboot整合分页插件PageHelper](https://blog.csdn.net/weixin_42408447/article/details/117528795)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Springboot集成pagehelper 分页插件](https://blog.csdn.net/weixin_58696998/article/details/124397756)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值