注意,本文的前提是配置正常,即依据官方文档配置有使用了 pagehelper springboot starter的情况。以下问题分析都是基于pagehelper springboot starter 1.2.10(经查看目前最新的1.2.13源码,发现仍有同样问题)
1. starter pom
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot</artifactId>
<version>1.2.10</version>
</parent>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<name>pagehelper-spring-boot-starter</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
</dependency>
</dependencies>
</project>
从上面的pom中可以看出,这里已经依赖了pagehelper-spring-boot-autoconfigure、pagehelper,不需要再重复引入,网上大部分都是说少了引入这两个jar,实属瞎扯淡(starter版本一致的情况下)。下面开始贴自动配置源码
1.1 PageHelperAutoConfiguration源码
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 abel533@gmail.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.github.pagehelper.autoconfigure;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Properties;
/**
*
* @author liuzh
*/
@Configuration
@ConditionalOnBean(SqlSessionFactory.class)/**注意这里,使用的ConditionalOnBean,问题所在*/
@EnableConfigurationProperties(PageHelperProperties.class)
@AutoConfigureAfter(MybatisAutoConfiguration.class)
public class PageHelperAutoConfiguration {
@Autowired
private List<SqlSessionFactory> sqlSessionFactoryList;
@Autowired
private PageHelperProperties properties;
/**
*
* @return
*/
@Bean
@ConfigurationProperties(prefix = PageHelperProperties.PAGEHELPER_PREFIX)
public Properties pageHelperProperties() {
return new Properties();
}
/**
*
* @return
*/
@PostConstruct
public void addPageInterceptor() {
PageInterceptor interceptor = new PageInterceptor();
Properties properties = new Properties();
properties.putAll(pageHelperProperties());
//这行我没看出有什么存在的意义,后面删掉
properties.putAll(this.properties.getProperties());
interceptor.setProperties(properties);
for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
}
}
}
问题就出现在这里,@ConditionalOnBean(SqlSessionFactory.class)这个注解是存在先后顺序问题的,当在开始构建PageHelperAutoConfiguration的时候,发现没有SqlSessionFactory这个bean,那就不会初始化了,配置自然也就失效了。就会出现一种令人抓狂的现象,TM我明明按照规范配置,为什么偏偏就不起效,不管怎么分页,查出来的都是全部?罪魁祸首就在这里。
1.2 解决方案
自定义自动配置类来添加PageInterceptor
/**
*1、删除PageHelperProperties 的注入,恕我直言我没搞明白这个是干嘛用的,跟pageHelperProperties()返回的数据完全一样。
2、删除properties.putAll(this.properties.getProperties()); 原因同上。
3、删掉@EnableConfigurationProperties(PageHelperProperties.class) 原因同上。
*4、@ConditionalOnBean(SqlSessionFactory.class)改用@AutoConfigureAfter({SqlSessionFactory.class})
*
* @author zhanghaowen
*/
@Configuration
@AutoConfigureAfter({SqlSessionFactory.class,MybatisAutoConfiguration.class})
public class MyPageHelperAutoConfiguration {
private static final String PAGEHELPER_PREFIX = "pagehelper";
@Autowired
private List<SqlSessionFactory> sqlSessionFactoryList;
@Bean
@ConfigurationProperties(prefix = PAGEHELPER_PREFIX)
public Properties pageHelperProperties() {
return new Properties();
}
@PostConstruct
public void addPageInterceptor() {
PageInterceptor interceptor = new PageInterceptor();
Properties properties = new Properties();
properties.putAll(pageHelperProperties());
interceptor.setProperties(properties);
for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
}
}
}
然后去掉pom中pagehelper springboot starter 1.2.10依赖,手工引入pagehelper包,这里1.2.10的starter用的是5.1.8的pagehelper,所以直接沿用
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.8</version>
</dependency>
启动项目,重新执行查询,成功解决问题。