精尽MyBatis源码分析 - Spring-Boot-Starter 源码分析
该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址、Mybatis-Spring 源码分析 GitHub 地址、Spring-Boot-Starter 源码分析 GitHub 地址)进行阅读
MyBatis 版本:3.5.2
MyBatis-Spring 版本:2.0.3
MyBatis-Spring-Boot-Starter 版本:2.1.4
在《MyBatis-Spring源码分析》文档中对 Spring 集成 MyBatis 的方案进行了分析,MyBatis-Spring
让你能够在 Spring 项目中方便地使用 MyBatis,随着 Spring Boot 框架受到业界的广泛关注,有越来越多企业使将它使用到正式的生产环境,它支持整合其他组件,让你能够在 Spring Boot 项目中更加方便地使用其他组件
当然,MyBatis 也提供了整合到 Spring Boot 的方案 Spring-Boot-Starter
,能够让你快速的在 Spring Boot 上面使用 MyBatis,那么我们来看看这个 Spring-Boot-Starter 子项目 是如何将 MyBatis 集成到 Spring 中的
在开始读这篇文档之前,需要对 Spring 有一定的了解,其中Spring-Boot-Starter
基于 MyBatis-Spring
来实现的,所以可以查看我的另一篇《MyBatis-Spring源码分析》文档来了解 MyBatis-Spring
,可以结合我的源码注释(Spring-Boot-Starter 源码分析 GitHub 地址)进行阅读
简述
MyBatis 的 Spring-Boot-Starter
子项目我们主要看到两个模块
- mybatis-spring-boot-starter:定义了一个 pom 文件,引入 MyBatis 相关依赖
- mybatis-spring-boot-autoconfigure:MyBatis 整合到 Spring Boot 的具体实现
主要涉及到的几个类:
org.mybatis.spring.boot.autoconfigure.MybatisProperties
:MyBatis 的配置类,注入 Spring Boot 的配置文件中 MyBatis 的相关配置org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
:实现 InitializingBean 接口,MyBatis 自动配置类,用于初始化 MyBatis,核心类
大致逻辑如下:
通过 MybatisAutoConfiguration
这个自动配置类,再加上 MybatisProperties
的配置信息,生成 SqlSessionFactory 和 SqlSessionTemplate 类,完成初始化,通过 @MapperScan 注解指定 Mapper 接口
配置示例
mybatis:
type-aliases-package: tk.mybatis.simple.model
mapper-locations: classpath:mapper/*.xml
config-location: classpath:mybatis-config.xml
在application.yml
中添加上面三个MyBatis的相关配置即可,然后在启动类上面添加@MapperScan
注解指定 Mapper 接口所在包路径即可
注意:你还需要定义一个 DataSource 数据源,可选 Druid
、HikariCP
等数据库连接池,这里就不讲述如何使用了
MybatisProperties
org.mybatis.spring.boot.autoconfigure.MybatisProperties
:MyBatis 的配置类,通过 Spring Boot 中的 @ConfigurationProperties
注解,注入 MyBatis 的相关配置,代码如下:
@ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX)
public class MybatisProperties {
public static final String MYBATIS_PREFIX = "mybatis";
private static final ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
/**
* Location of MyBatis xml config file.
* mybatis-config.xml 配置文件的路径
*/
private String configLocation;
/**
* Locations of MyBatis mapper files.
* XML 映射文件的路径
*/
private String[] mapperLocations;
/**
* Packages to search type aliases. (Package delimiters are ",; \t\n")
* 需要设置别名的包路径
*/
private String typeAliasesPackage;
/**
* The super class for filtering type alias. If this not specifies, the MyBatis deal as type alias all classes that
* searched from typeAliasesPackage.
*/
private Class<?> typeAliasesSuperType;
/**
* Packages to search for type handlers. (Package delimiters are ",; \t\n")
*/
private String typeHandlersPackage;
/**
* Indicates whether perform presence check of the MyBatis xml config file.
*/
private boolean checkConfigLocation = false;
/**
* Execution mode for {@link org.mybatis.spring.SqlSessionTemplate}.
*/
private ExecutorType executorType;
/**
* The default scripting language driver class. (Available when use together with mybatis-spring 2.0.2+)
*/
private Class<? extends LanguageDriver> defaultScriptingLanguageDriver;
/**
* Externalized properties for MyBatis configuration.
*/
private Properties configurationProperties;
/**
* A Configuration object for customize default settings. If {@link #configLocation} is specified, this property is
* not used.
*/
@NestedConfigurationProperty
private Configuration configuration;
/**
* 获取 XML 映射文件路径下的资源对象
*
* @return Resource 资源数组
*/
public Resource[] resolveMapperLocations() {
return Stream.of(Optional.ofNullable(this.mapperLocations).orElse(new String[0]))
.flatMap(location -> Stream.of(getResources(location))).toArray(Resource[]::new);
}
/**
* 获取某个路径下的资源
*
* @param location 路径
* @return Resource 资源数组
*/
private Resource[] getResources(String location) {
try {
return resourceResolver.getResources(location);
} catch (IOException e) {
return new Resource[0];
}
}
}
configLocation
:mybatis-config.xml 配置文件的路径mapperLocations
:XML 映射文件的路径typeAliasesPackage
:需要设置别名的包路径,多个以, ; \t\n
分隔typeAliasesSuperType
:需要设置别名的父 Class 类型typeHandlersPackage
:类型处理器的包路径checkConfigLocation
:检查 mybatis-config.xml 配置文件是否存在executorType
:Executor 执行器类型,默认 SIMPLEdefaultScriptingLanguageDriver
:设置默认的 LanguageDriver 语言驱动类,默认为 XMLLanguageDriver
其中定义了前缀为mybatis
,说明你可以在 Spring Boot 项目中的 application.yml 配置文件中,以该前缀定义 MyBatis 的相关属性
我们通常添加前面三个配置就可以了
这里注意到仅添加了 @ConfigurationProperties 注解,在作为 Spring Bean 注入到 Spring 容器中时,会将相关配置注入到属性中,但是这个注解不会将该类作为 Spring Bean 进行注入,需要结合 @Configuration 注解或者其他注解一起使用
SpringBootVFS
org.mybatis.spring.boot.autoconfigure.SpringBootVFS
:MyBatis 需要使用到的虚拟文件系统,用于替代 MyBatis 的 org.apache.ibatis.io.DefaultVFS
默认类
使用 Spring Boot 提供的 PathMatchingResourcePatternResolver 解析器,获取到指定路径下的 Resource 资源,代码如下:
public class SpringBootVFS extends VFS {
private final ResourcePatternResolver resourceResolver;
public SpringBootVFS() {
this.resourceResolver = new PathMatchingResourcePatternResolver(getClass().getClassLoader());
}
@Override
public boolean isValid() {
return true;
}
@Override
protected List<String> list(URL url, String path) throws IOException {
String urlString = url.toString();
String baseUrlString = urlString.endsWith("/") ? urlString : urlString.concat("/");
Resource[] resources = resourceResolver.getResources(baseUrlString + "**/*.class");
return Stream.of(resources).map(resource -> preserveSubpackageName(baseUrlString, resource, path))
.collect(Collectors.toList());
}
private static String preserveSubpackageName(final String baseUrlString, final Resource resource,
final String rootPath) {
try {
return rootPath + (rootPath.endsWith("/") ? "" : "/")
+ resource.getURL().toString().substring(baseUrlString.length());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
MybatisAutoConfiguration
org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
:实现 InitializingBean 接口,MyBatis 自动配置类,用于初始化 MyBatis,核心类
构造方法
@org.springframework.context.annotation.Configuration
@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
@ConditionalOnSingleCandidate(DataSource.class)
@EnableConfigurationProperties(MybatisProperties.class)
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class })
public class MybatisAutoConfiguration implements InitializingBean {
private static final Logger logger = LoggerFactory.getLogger(MybatisAutoConfiguration.class);
/**
* MyBatis 配置信息
*/
private final MybatisProperties properties;
private final Interceptor[] interceptors;
private final TypeHandler[] typeHandlers;
private final LanguageDriver[] languageDrivers;
private final ResourceLoader resourceLoader;
private final DatabaseIdProvider databaseIdProvider;
private final List<ConfigurationCustomizer> configurationCustomizers;
public MybatisAutoConfiguration(MybatisProperties properties, ObjectProvider<Interceptor[]> interceptorsProvider,
ObjectProvider<TypeHandler[]> typeHandlersProvider, ObjectProvider<LanguageDriver[]> languageDriversProvider,
ResourceLoader resourceLoader, ObjectProvider<DatabaseIdProvider> databaseIdProvider,
ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider) {
this.properties = properties;
this.interceptors = interceptorsProvider.getIfAvailable();
this.typeHandlers = typeHandlersProvider.getIfAvailable();
this.languageDrivers = languageDriversProvider.getIfAvailable();
this.resourceLoader = resourceLoader;
this.databaseIdProvider = databaseIdProvider.getIfAvailable();
this.configurationCustomizers = configurationCustomizersProvider.getIfAvailable();
}
}
我们主要来看到类上面定义的几个注解:
@Configuration
:可以当作一个 Spring Bean 注入到 Spring 上下文中@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
保证存在 value 中所有的 Class 对象,以确保可以创建它们的实例对象,这里就保证 SqlSessionFactory 和 SqlSessionFactoryBean 都能够被创建
@ConditionalOnSingleCandidate(DataSource.class)
保证存在 value 类型对应的 Bean,这里确保已经存在一个 DataSource 数据源对象
@EnableConfigurationProperties(MybatisProperties.class)
注入 value 中所有的类型的 Bean,这里会让 MybatisProperties 作为 Spring Bean 注入到 Spring 上下文中
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, MybatisLanguageDriverAutoConfiguration.class })
在加载 value 中的所有类之后注入当前 Bean,会先注入 DataSourceAutoConfiguration 和 MybatisLanguageDriverAutoConfiguration 两个类(感兴趣的可以去看看,我没搞懂这两个类
精尽MyBatis源码分析 - Spring-Boot-Starter 源码分析的更多相关文章
- 头秃了,二十三张图带你从源码了解Spring Boot 的启动流程~
持续原创输出,点击上方蓝字关注我 目录 前言 源码版本 从哪入手? 源码如何切分? 如何创建SpringApplication? 设置应用类型 设置初始化器(Initializer) 设置监听器(Li ...
- spring boot @Value源码解析
Spring boot 的@Value只能用于bean中,在bean的实例化时,会给@Value的属性赋值:如下面的例子: @SpringBootApplication @Slf4j public c ...
- Spring Boot 实战与原理分析视频课程
Spring Boot 实战与原理分析视频课程 链接:https://pan.baidu.com/share/init?surl=PeykcoeqZtd1d9lN9V_F-A 提取码: 关注公众号[G ...
- spring boot启动原理步骤分析
spring boot最重要的三个文件:1.启动类 2.pom.xml 3.application.yml配置文件 一.启动类->main方法 spring boot启动原理步骤分析 1.spr ...
- spring boot应用启动原理分析
spring boot quick start 在spring boot里,很吸引人的一个特性是可以直接把应用打包成为一个jar/war,然后这个jar/war是可以直接启动的,不需要另外配置一个We ...
- Spring Boot应用启动原理分析(转)
在spring boot里,很吸引人的一个特性是可以直接把应用打包成为一个jar/war,然后这个jar/war是可以直接启动的,不需要另外配置一个Web Server. 如果之前没有使用过sprin ...
- Spring Boot Starter 介绍
http://www.baeldung.com/spring-boot-starters 作者:baeldung 译者:http://oopsguy.com 1.概述 依赖管理是任何复杂项目的关键部分 ...
- Spring Boot (一): Spring Boot starter自定义
前些日子在公司接触了spring boot和spring cloud,有感于其大大简化了spring的配置过程,十分方便使用者快速构建项目,而且拥有丰富的starter供开发者使用.但是由于其自动化配 ...
- spring -boot s-tarter 详解
Starter POMs是可以包含到应用中的一个方便的依赖关系描述符集合.你可以获取所有Spring及相关技术的一站式服务,而不需要翻阅示例代码,拷贝粘贴大量的依赖描述符.例如,如果你想使用Sprin ...
- SpringBoot 之Spring Boot Starter依赖包及作用
Spring Boot 之Spring Boot Starter依赖包及作用 spring-boot-starter 这是Spring Boot的核心启动器,包含了自动配置.日志和YAML. spri ...
随机推荐
- 群晖DS218+部署kafka
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 2020 10月CUMTCTF wp
华为杯 × 签到杯√ 论比赛过程来说没什么很大收获 但看师傅们的wp感触很多 赛后复现慢慢学吧 Web babyflask flask ssti模板注入: payload{{key}}发现[]以及类似 ...
- 快来,我悄悄的给你说几个HashCode的破事。
这是why技术的第 72 篇原创文章 Hash冲突是怎么回事 在这个文章正式开始之前,先几句话把这个问题说清楚了:我们常说的 Hash 冲突到底是怎么回事? 直接上个图片: 你说你看到这个图片的时候想 ...
- python机器学习实现K-近邻算法(KNN)
机器学习 K-近邻算法(KNN) 关注公众号"轻松学编程"了解更多. 以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter notebook 后打开浏览器输入网址htt ...
- 使用scrapy框架模拟登录
scrapy模拟登录 注意:模拟登陆时,必须保证settings.py里的COOKIES_ENABLED(Cookies中间件) 处于开启状态 COOKIES_ENABLED = True 或# CO ...
- .NET redis 客户端开源组件 FreeRedis (继 CSRedisCore 之后重写)
什么是 FreeRedis FreeRedis 是一款 .NET redis 客户端开源组件,以 MIT 协议开源托管于 github,目前支持 .NET 5..NETCore 2.1+..NETFr ...
- 使用Python虚拟环境
python 的虚拟环境可以为一个 python 项目提供独立的解释环境.依赖包等资源,既能够很好的隔离不同项目使用不同 python 版本带来的冲突,而且还能方便项目的发布. virtualenv ...
- C++ 数据结构 3:树和二叉树
1 树 1.1 定义 由一个或多个(n ≥ 0)结点组成的有限集合 T,有且仅有一个结点称为根(root),当 n > 1 时,其余的结点分为 m (m ≥ 0)个互不相交的有限集合T1,T2, ...
- Spark Shuffle机制详细源码解析
Shuffle过程主要分为Shuffle write和Shuffle read两个阶段,2.0版本之后hash shuffle被删除,只保留sort shuffle,下面结合代码分析: 1.Shuff ...
- js匀速运动框架案例
点击"开始运动"按钮,红色的#red区块开始向右匀速运动,抵达到黑色竖线位置自动停止,再次点击"开始运动"#red区块也不会再运动.同时为了便于后期维护,要求运 ...
- 头秃了,二十三张图带你从源码了解Spring Boot 的启动流程~