深入源码分析Spring boot 集成Pagehelper

引入依赖:

<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拦截器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员路同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值