springboot分页插件的问题

1:maven依赖的问题 此类原因是与pom.xml文件中引入的分页依赖有关,由于springboot本身集成pagerhelper的分页插件,只需要引入如下依赖即可

<!-- spring-boot mybatis pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.10</version> </dependency> 如引入的为如下依赖,需要添加Bean注入(如何添加请自行百度)

<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.10</version> </dependency> 2:执行PageHelper.startPage(int pageNum, int pageSize)之后没有紧跟分页查询,而是先执行了其他查询 如下初始化分页器后,应该紧跟mybatis的分页查询语句,方法中如有其他查询需求,需要在其他查询完成后,再执行PageHelper.startPage(int pageNum, int pageSize)方法

public PageInfo<R> page(Map<String, ? extends Object> map) {
//获取第1页,10条内容,默认查询总数count
  PageHelper.startPage(Integer.parseInt(map.get("pageNum").toString()), Integer.parseInt(map.get("pageSize").toString()));
  String sql = String.format("%s%s",sqlMapping , map.get("mapping")==null?"getPageObjList" : map.get("mapping")) ;
List<R> l = sqlSessionTemplate.selectList(sql , map);
return new PageInfo<R>(l);
}

3:没有配置mybatis的分页拦截器(也是我遇到的问题) 当拦截器没有配置的时候,每次进行List查询都会返回全部结果数据,此时需要在启动类中注入拦截器类

@Bean
public Interceptor[] plugins() {
return new Interceptor[]{new PageInterceptor()};
}

或者在MyBatis的配置文件mybatis-config.xml中添加如下代码

<configuration> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"/> </plugins> </configuration>

posted @ 2023-04-27 17:13  启豪  阅读(40)  评论(0编辑  收藏  举报