该系列文档是本人在学习 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 数据源,可选 DruidHikariCP等数据库连接池,这里就不讲述如何使用了

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 执行器类型,默认 SIMPLE
  • defaultScriptingLanguageDriver:设置默认的 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 源码分析的更多相关文章

    1. 头秃了,二十三张图带你从源码了解Spring Boot 的启动流程~

      持续原创输出,点击上方蓝字关注我 目录 前言 源码版本 从哪入手? 源码如何切分? 如何创建SpringApplication? 设置应用类型 设置初始化器(Initializer) 设置监听器(Li ...

    2. spring boot @Value源码解析

      Spring boot 的@Value只能用于bean中,在bean的实例化时,会给@Value的属性赋值:如下面的例子: @SpringBootApplication @Slf4j public c ...

    3. Spring Boot 实战与原理分析视频课程

      Spring Boot 实战与原理分析视频课程 链接:https://pan.baidu.com/share/init?surl=PeykcoeqZtd1d9lN9V_F-A 提取码: 关注公众号[G ...

    4. spring boot启动原理步骤分析

      spring boot最重要的三个文件:1.启动类 2.pom.xml 3.application.yml配置文件 一.启动类->main方法 spring boot启动原理步骤分析 1.spr ...

    5. spring boot应用启动原理分析

      spring boot quick start 在spring boot里,很吸引人的一个特性是可以直接把应用打包成为一个jar/war,然后这个jar/war是可以直接启动的,不需要另外配置一个We ...

    6. Spring Boot应用启动原理分析(转)

      在spring boot里,很吸引人的一个特性是可以直接把应用打包成为一个jar/war,然后这个jar/war是可以直接启动的,不需要另外配置一个Web Server. 如果之前没有使用过sprin ...

    7. Spring Boot Starter 介绍

      http://www.baeldung.com/spring-boot-starters 作者:baeldung 译者:http://oopsguy.com 1.概述 依赖管理是任何复杂项目的关键部分 ...

    8. Spring Boot (一): Spring Boot starter自定义

      前些日子在公司接触了spring boot和spring cloud,有感于其大大简化了spring的配置过程,十分方便使用者快速构建项目,而且拥有丰富的starter供开发者使用.但是由于其自动化配 ...

    9. spring -boot s-tarter 详解

      Starter POMs是可以包含到应用中的一个方便的依赖关系描述符集合.你可以获取所有Spring及相关技术的一站式服务,而不需要翻阅示例代码,拷贝粘贴大量的依赖描述符.例如,如果你想使用Sprin ...

    10. SpringBoot 之Spring Boot Starter依赖包及作用

      Spring Boot 之Spring Boot Starter依赖包及作用 spring-boot-starter 这是Spring Boot的核心启动器,包含了自动配置.日志和YAML. spri ...

    随机推荐

    1. SpringMVC的@InitBinder参数转换

      @Controller @RequestMapping("/index") public class IndexController { /** * 解决前端传递的日期参数验证异常 ...

    2. 对于类似经销商的实体中Place(CBM_PALCE_ID = NULL)的情况,如何获取省市信息(限foton)

      在库里像上述描述的数据很多,这种情况需要拿fence里的经纬度,反查省市 String[] strArr = data.getFence().getValue().split(";" ...

    3. Spring Boot 加载application.properties顺序

      1.准备四份application.properties a.项目根目录下config/application.properties ,内容为:  test.user.name = a b.项目根目录 ...

    4. 【CF1445C】Divison 题解

      原题链接 题意简介 给出两个正整数 p 和 q,要求一个最大的 x 使 p 可被 x 整除,而 q 不可被 x 整除. 其中,\(1 \leq p \leq 10^{18} ; 2 \leq q \l ...

    5. 【Redis】Redis 持久化之 RDB 与 AOF 详解

      一.Redis 持久化 我们知道Redis的数据是全部存储在内存中的,如果机器突然GG,那么数据就会全部丢失,因此需要有持久化机制来保证数据不会一位宕机而丢失.Redis 为我们提供了两种持久化方案, ...

    6. Java_多线程实现

      一个类两个接口 Tread类: 使用时继承Thread类 Runnable接口: 使用时实现Runnable接口 Callable接口: 使用时实现Callable接口 由于类只能单继承, 接口可以多 ...

    7. 【故障公告】Memcached 的“惹祸”,不知在为谁背锅

      在 .NET 5.0 背锅 . Memcached 的惹祸 .缓存雪崩之后,我们没有找到问题的真正原因,我们知道没有找到根源的故障总是会再次光临的,不是在这周就是在下周,也许就在双11前后. 就在今天 ...

    8. 使用css控制table的cellspacing和cellpadding属性

      HTML默认的表格样式之间有间隙,每次为了解决这些问题,总要在table标签里添加cellspacing和cellpadding,你是否也很厌倦这样的写法, 那么有没有对应的CSS属性能达到相同的效果 ...

    9. 面试官:Redis 主从复制时网络开小差了怎么整?

      上周因为实在太忙就认认真真写了一篇水文,吹了一下自己过去的经历,反响竟然超出了我的预期,并且后台还有读者留言表示想看续集的.哈哈,果然大家还是对水文更有热情. 这期我们继续回到之前的 Redis 话题 ...

    10. nginx&http 第三章 ngx 1-http ngx_http_wait_request_handler

      对于活跃的 HTTP 连接,在执行连接建立回调函数 ngx_http_init_connection 的过程中会执行 ngx_http_wait_request_handler 回调函数, 负责 HT ...