免费资源网,https://freexyz.cn/
目录
  • SpringBoot整合PageHelper分页无效的常见原因
    • 1.maven依赖的问题
    • 2.执行PageHelper.startPage(int pageNum, int pageSize)
    • 3.没有配置mybatis的分页拦截器(也是我遇到的问题)
  • 总结

    SpringBoot整合PageHelper分页无效的常见原因

    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>

    总结

    以上就是综合网上大家遇到的springboot使用pagehelper进行分页时,遇到查询出全部数据而没有进行分页的常见问题及解决方案。

    这些仅为个人经验,希望能给大家一个参考,也希望大家多多支持。

    免费资源网,https://freexyz.cn/
    声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。