使用mybatis,对于用到的组件需要明确其作用,这样才能灵活用好组件,熟悉原理,能够更好的评估组件的作用和排查问题。
mybatis 周边
按照依赖的顺序,列出如下,其中pagehelper 和mapper 没有依赖关系
- mybatis mybatis核心库
- mybatis-spring mybatis集成spring库
- mybatis-spring-boot-autoconfigure mybatis spring-boot自动化配置库
- mybatis-spring-boot-starter 启用spring-boot自动化配置库
- mapper tk通用Mapper库
- mapper-spring-boot-autoconfigure tk通用Mapper spring-boot自动化配置库
- mapper-spring-boot-starter 启用spring-boot自动化配置库
- pagehelper tk通用分页拦截器
- pagehelper-spring-boot-autoconfigure tk通用分页拦截器 spring-boot自动化配置库
- pagehelper-spring-boot-starter 启用spring-boot自动化配置库
mybatis组件核心类
对应上述的组件库,关键类如下:
- org.apache.ibatis.session.SqlSessionFactory
- org.mybatis.spring.SqlSessionFactoryBean, org.mybatis.spring.mapper.MapperFactoryBean
- org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration,
- 无
- tk.mybatis.mapper.mapperhelper.MapperHelper
- com.github.pagehelper.autoconfigure.MapperAutoConfiguration
- 无
- com.github.pagehelper.PageInterceptor
- com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration
组件依赖关系spring-boot体现
spring-boot自动配置关键类如下,这三者之间有明确的加载解析顺序,保障容器的正常启动和增强特性的添加。
// mybatis-spring组件依赖mybatis组件,spring-boot自动配置依赖spring-mybatis
@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
@ConditionalOnBean(DataSource.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class MybatisAutoConfiguration {}
// mapper 依赖spring-mybatis,该组件基于Mapper bean的能力增强
@ConditionalOnBean(SqlSessionFactory.class)
@AutoConfigureAfter(MybatisAutoConfiguration.class)
public class MapperAutoConfiguration {}
// pagehelper 依赖spring-mybatis,该组件基于Mapper bean的能力增强
@ConditionalOnBean(SqlSessionFactory.class)
@AutoConfigureAfter(MybatisAutoConfiguration.class)
public class PageHelperAutoConfiguration implements EnvironmentAware {}