引入依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>
pagehelper分页插件 github网址:https://github.com/pagehelper/Mybatis-PageHelper
PageHelperAutoConfiguration
spring.factories文件 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration
@Configuration @ConditionalOnBean(SqlSessionFactory.class) @EnableConfigurationProperties(PageHelperProperties.class) @AutoConfigureAfter(MybatisAutoConfiguration.class) @Lazy(false) public class PageHelperAutoConfiguration
可以在配置文件里自定义配置pagehelper,可以参考PageHelperProperties 定义参数
dialect helperDialect autoRuntimeDialect autoDialect reasonable pageSizeZero rowBoundsWithCount offsetAsPageNum
在PageHelperAutoConfiguration配置类里,在addPageInterceptor方法里会将你在配置文件里的配置导入拦截器
@PostConstruct public void addPageInterceptor() { PageInterceptor interceptor = new PageInterceptor(); Properties properties = new Properties(); //先把一般方式配置的属性放进去 properties.putAll(pageHelperProperties()); //在把特殊配置放进去,由于close-conn 利用上面方式时,属性名就是 close-conn 而不是 closeConn,所以需要额外的一步 properties.putAll(this.properties.getProperties()); interceptor.setProperties(properties); for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) { org.apache.ibatis.session.Configuration configuration = sqlSessionFactory.getConfiguration(); if (!containsInterceptor(configuration, interceptor)) { configuration.addInterceptor(interceptor); } } }/** * 接受分页插件额外的属性 * * @return */ @Bean @ConfigurationProperties(prefix = PageHelperProperties.PAGEHELPER_PREFIX) public Properties pageHelperProperties() { return new Properties(); }
由此可以看出PageHelperAutoConfiguration主要就是将自定义配置的拦截器属性加入到Configuration拦截器链中。
下面分析下pagehelper-spring-boot-starter带有的pageHelper包,使用pagehelper-spring-boot-starter会自动将pageHelper拦截器加入mybatis的Configuration中

pagehelper-spring-boot-starter包含了pagehelper包<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> </dependency>
在pagehelper包中,主要实现了mapper文件执行sql时判断是否查询统计总数以及对分页sql拼装查询当前页数记录,封装成Page实体返回结果。下面进入PageHelper包一探究竟;
PageHelper
目录结构

cache包下主要是定义的抽象缓存工厂CacheFactory 以及Guava Cache 和 MyBatis Cache
dialect包下定义了一些方言 如Mysql数据库,oracle,sqlServer,Hsqldb等
page包下包括分页参数信息,分页接口方法
parser包下 就是Sql解析类 包含count order
util包下就是一些工具类 主要有ExecutorUtil sql执行工具类,PageObjectUtil分页参数对象工具类
见识下别人的工厂设计模式,可以借鉴使用
/**
* 创建 SQL 缓存
*
* @param sqlCacheClass
* @return
*/
public static <K, V> Cache<K, V> createCache(String sqlCacheClass, String prefix, Properties properties) {
if (StringUtil.isEmpty(sqlCacheClass)) {
//为空时,使用pageHelper cache包下定义的两个缓存
try {
Class.forName("com.google.common.cache.Cache");
return new GuavaCache<K, V>(properties, prefix);
} catch (Throwable t) {
return new SimpleCache<K, V>(properties, prefix);
}
} else {
//如果用户自定义了cache缓存,则走下面 给出 Cache接口交给开发者去自定义扩展实现
try {
Class<? extends Cache> clazz = (Class<? extends Cache>) Class.forName(sqlCacheClass);
try {
//通过有参构造函数来反射创建实体类
Constructor<? extends Cache> constructor = clazz.getConstructor(Properties.class, String.class);
return constructor.newInstance(properties, prefix);
} catch (Exception e) {
return clazz.newInstance();
}
} catch (Throwable t) {
throw new PageException("Created Sql Cache [" + sqlCacheClass + "] Error", t);
}
}
}
pageHelper对sql分页查询可以参考:pageHelper拦截器