在这里插入图片描述
没有使用pagehelper接口能正常运行,但是使用了就报空指针异常,找了好久原因,最后从版本问题来解决,用了一系列pagehelper-spring-boot-starter,依然包空指针
如:

<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.4.1</version>
        </dependency>

然后导入pageHepler空指针解决了,但是查询的是所有的,分页没成功

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.0.0</version>
        </dependency>

最后经过n多小时痛苦死磨烂打,根据一个大佬的方法得到解决.https://ask.csdn.net/questions/7397880

因为项目引入了spring security,所以User对象里面要加上getAuthorities方法,使用PageHelper.startPage(page.getPage(), page.getCount(), page.getOrderBy());进行分页的时候底层有个操作是将user对象里面的属性put进map的操作,但是getRole()为空值,一开始我没有加上判断空操作,导致到authorities的时候报错了
在这里插入图片描述

最后附上pageHelper的配置和使用

pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql

使用:直接在你要分页的查询语句上写上即可:

PageHelper.startPage(currentPage,size);
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐