栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

深入源码分析Spring boot 集成Pagehelper

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

深入源码分析Spring boot 集成Pagehelper

引入依赖:


    com.github.pagehelper
    pagehelper-spring-boot-starter

 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);
        }
    }
}
@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包

    com.github.pagehelper
    pagehelper

 在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分页参数对象工具类

 见识下别人的工厂设计模式,可以借鉴使用

public static  Cache createCache(String sqlCacheClass, String prefix, Properties properties) {
    if (StringUtil.isEmpty(sqlCacheClass)) {
   //为空时,使用pageHelper cache包下定义的两个缓存
        try {
            Class.forName("com.google.common.cache.Cache");
            return new GuavaCache(properties, prefix);
        } catch (Throwable t) {
            return new SimpleCache(properties, prefix);
        }
    } else {
   //如果用户自定义了cache缓存,则走下面 给出 Cache接口交给开发者去自定义扩展实现
        try {
            Class clazz = (Class) Class.forName(sqlCacheClass);
            try {
    //通过有参构造函数来反射创建实体类
                Constructor 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拦截器

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/871966.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号