Mybatis源码解读-SpringBoot中配置加载和Mapper的生成

本文 mybatis-spring-boot探讨在springboot工程中mybatis相关对象的注册与加载。

建议先了解mybatis在spring中的使用和springboot自动装载机制,再看此文章。

@MapperScan@Mapper能一起用吗?

在讨论自动装配方式之前,先看看mybatis最简洁的demo

public static void main(String[] args) throws Exception {
    // 配置文件路径
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    // 1.读取配置,创建SqlSessionFactory
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    // 2.通过工厂获取SqlSession
    SqlSession session = sqlSessionFactory.openSession();
    try {
        // 3.获取mapper代理对象
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        // 4.执行查询,此处才真正连接数据库
        System.out.println(mapper.selectByName("张三"));
    } finally {
        // 5.关闭连接
        session.close();
    }
}

可以看到,首先需要创建SqlSessionFactory和SqlSession,在springboot中,这两者通过自动装配完成。

在mybatis-spring-boot-autoconfigure-x.x.x.jar的spring.factories中,可以看到自动装配注入了 MybatisAutoConfiguration

public class MybatisAutoConfiguration implements InitializingBean {
  ......

  @Bean
  @ConditionalOnMissingBean
  public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    ......

  }

  @Bean
  @ConditionalOnMissingBean
  public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
    ExecutorType executorType = this.properties.getExecutorType();
    if (executorType != null) {
      return new SqlSessionTemplate(sqlSessionFactory, executorType);
    } else {
      return new SqlSessionTemplate(sqlSessionFactory);
    }
  }
}

SqlSessionTemplateSqlSession的子类,所以现在二者都有了。

Mapper的扫描分两种方式讨论

  1. @MapperScan方式
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(MapperScannerRegistrar.class)
@Repeatable(MapperScans.class)
public @interface MapperScan {

}

可以看到,导入了MapperScannerRegistrar类

public class MapperScannerRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {
  @Override
  public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    AnnotationAttributes mapperScanAttrs = AnnotationAttributes
        .fromMap(importingClassMetadata.getAnnotationAttributes(MapperScan.class.getName()));
    if (mapperScanAttrs != null) {
      registerBeanDefinitions(importingClassMetadata, mapperScanAttrs, registry,
          generateBaseBeanName(importingClassMetadata, 0));
    }
  }

  void registerBeanDefinitions(AnnotationMetadata annoMeta, AnnotationAttributes annoAttrs,
      BeanDefinitionRegistry registry, String beanName) {

    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class);
    ......

    registry.registerBeanDefinition(beanName, builder.getBeanDefinition());
  }
}

因为MapperScannerRegistrar实现了ImportBeanDefinitionRegistrar接口,所以会被调用registerBeanDefinitions方法,最后注册MapperScannerConfigurer 咱们先记住MapperScannerConfigurer这个类,去看看@Mapper的方式
2. @Mapper方式 在 MybatisAutoConfiguration中,有这么一段代码

@org.springframework.context.annotation.Configuration
// 如果满足条件,则导入AutoConfiguredMapperScannerRegistrar
@Import(AutoConfiguredMapperScannerRegistrar.class)
// 如果MapperFactoryBean和MapperScannerConfigurer都没注册,则满足条件
@ConditionalOnMissingBean({ MapperFactoryBean.class, MapperScannerConfigurer.class })
public static class MapperScannerRegistrarNotFoundConfiguration implements InitializingBean {

  ......

}

我们在@MapperScan方式看到,是已经注册了MapperScannerConfigurer类的。所以,@MapperScan会覆盖@Mapper 继续看看 AutoConfiguredMapperScannerRegistrar

public static class AutoConfiguredMapperScannerRegistrar
      implements BeanFactoryAware, EnvironmentAware, ImportBeanDefinitionRegistrar {

  @Override
  public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

    ......

    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class);
    builder.addPropertyValue("annotationClass", Mapper.class);
    ......

    registry.registerBeanDefinition(MapperScannerConfigurer.class.getName(), builder.getBeanDefinition());
  }

  ......

}

可以看到,同样是注册了MapperScannerConfigurer 也就是两种注解方式都是通过MapperScannerConfigurer扫描mapper注册的
3. 通用部分 继续追踪MapperScannerConfigurer的调用链

// MapperScannerConfigurer#postProcessBeanDefinitionRegistry
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
  ......

  ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
  ......

  // 注册过滤器(@Mapper和@MapperScan的区别体现在这里)
  scanner.registerFilters();
  // 开始扫描bean
  scanner.scan(
      StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
}

public void registerFilters() {
  boolean acceptAllInterfaces = true;

  // 如果指定了扫描类型(@Mapper走这里)
  // annotationClass在前面的AutoConfiguredMapperScannerRegistrar#registerBeanDefinitions被注入
  // 就是这段builder.addPropertyValue("annotationClass", Mapper.class);
  if (this.annotationClass != null) {
    addIncludeFilter(new AnnotationTypeFilter(this.annotationClass));
    acceptAllInterfaces = false;
  }

  ......

  // 如果没指定扫描类型,则扫描全部(@MapperScan走这里)
  if (acceptAllInterfaces) {
    addIncludeFilter((metadataReader, metadataReaderFactory) -> true);
  }

  // exclude package-info.java
  addExcludeFilter((metadataReader, metadataReaderFactory) -> {
    String className = metadataReader.getClassMetadata().getClassName();
    return className.endsWith("package-info");
  });
}

看完了过滤器的注册,继续回到扫描逻辑scanner.scan

// ClassPathMapperScanner#scan(String... basePackages) -->
// ClassPathMapperScanner#doScan(String... basePackages)
public Set doScan(String... basePackages) {
  // 扫描mapper(此时是原始对象)
  Set beanDefinitions = super.doScan(basePackages);

  if (beanDefinitions.isEmpty()) {
    LOGGER.warn(() -> "No MyBatis mapper was found in '" + Arrays.toString(basePackages)
        + "' package. Please check your configuration.");
  } else {
    // 通过MapperFactoryBean类将mapper对象转换成代理对象MapperProxy
    processBeanDefinitions(beanDefinitions);
  }

  return beanDefinitions;
}

@MapperScan@Mapper能一起用(不会报错),但是 @Mapper是没有效果的。

Original: https://www.cnblogs.com/konghuanxi/p/16277509.html
Author: 王谷雨
Title: Mybatis源码解读-SpringBoot中配置加载和Mapper的生成

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/583268/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 数据库简单查询

    简单查询 语法句式如下: SELECT filed1,filed2 … filedn FROM tablename [WHERE CONDITION11] [GROUP BY …

    Linux 2023年6月7日
    0137
  • [20220303]oracle如何定位使用library cache mutex 3.txt

    [20220303]oracle如何定位使用library cache mutex 3.txt –//这个问题实际上困扰我很久,我开始以为library cache b…

    Linux 2023年6月13日
    080
  • 新一代高性能USB转串口芯片CH342与CH343

    CH342与CH343是沁恒推出的第三代USB转串口产品,内部高度集成,外围精简,均提供VIO电源引脚,串口I/O支持独立供电。 CH342实现USB转两路高速异步串口,支持串口波…

    Linux 2023年6月7日
    0153
  • 在公司当上PD的心路历程

    前不久因为接了个新项目,我被选中当上PD也就是专门负责给客户演示,推进项目、录视频、写文档、做测试,因为我本来就需要测这些东西,熟悉算法、应用、固件,所以大部分人就觉得非我不可。 …

    Linux 2023年6月8日
    099
  • 小试牛刀:Linux中部署RabbitMQ

    一、下载地址 本人采用的是 RabbitMQ 3.8.20+ Erlang 23.3.4.16 1、Erlang下载:https://github.com/erlang/otp/r…

    Linux 2023年6月14日
    096
  • windows环境 php 连接 sql server

    下载扩展: ODBC Driver: 安装配置: 下载SQL Server的PHP扩展(Microsoft Drivers for PHP for SQL Server),连接里有…

    Linux 2023年6月7日
    0107
  • Linux 0.11源码阅读笔记-中断过程

    Linux 0.11源码阅读笔记-中断过程 是什么中断 中断发生时,计算机会停止当前运行的程序,转而执行中断处理程序,然后再返回原被中断的程序继续运行。中断包括硬件中断和软件中断,…

    Linux 2023年5月27日
    0117
  • 总结:弹性伸缩的五个条件与六个教训

    前言弹性伸缩是云计算时代给我们带来的一项核心技术红利,但是 IT 的世界中,没有一个系统功能可以不假思索的应用到所有的场景中。这篇文章,我们将应用企业级分布式应用服务-EDAS 的…

    Linux 2023年6月8日
    0102
  • ShardingSphere-proxy-5.0.0分布式哈希取模分片实现(四)

    一、说明 主要是对字符串的字段进行hash取模 二、修改配置文件config-sharding.yaml,并重启服务 # Licensed to the Apache Softwa…

    Linux 2023年6月14日
    081
  • 在Linux下安装Redis

    1. 下载Redis的压缩包 [root@spirit-of-fire ~]# wget http://download.redis.io/releases/redis-5.0.5…

    Linux 2023年6月14日
    0101
  • 11-K8S部署普罗米修斯

    K8S-Kubernetes 集群部署 Prometheus 和 Grafana 1.实验环境 控制节点/master01 192.168.80.20 工作节点/node01 19…

    Linux 2023年6月13日
    088
  • CentOS shell中的变量

    shell中的变量 变量的介绍 变量即变化的量,核心是”变”与”量”二字,变即变化,量即衡量状态。 量:是记录现实世界当中的某种状态…

    Linux 2023年6月7日
    096
  • [编译] 9、在Linux下搭建 nordic 最新基于 zephyr 的开发烧写环境

    前言 1、概述 2、安装工具 3、获取 nRF Connect SDK 源码 4、安装 Python modules 5、安装 toolchain 6、下载 nRF Command…

    Linux 2023年6月8日
    087
  • 国产银河麒麟Kylin V10操作系统

    今天想在国产银河麒麟Kylin V10操作系统中设置win+E显示资源管理器(我的电脑)的快捷键,首先需要判断麒麟操作系统使用的哪种桌面, 在终端Terminal中执行 echo …

    Linux 2023年6月14日
    0101
  • Redis安装(CentOS 8.5 64位)

    Redis安装 1. 准备工作 1.1 下载安装包 官网下载地址:https://redis.io/ 1.2 传输文件到服务器 使用ssh工具连接到服务器,把下载好的文件上传到服务…

    Linux 2023年6月14日
    097
  • 个人超级计算机

    这篇文章是回答一位用户的问题。 问:站长,我是一个在读研究生,正在学习分布式计算、高性能集群计算方面的知识,Laxcus分布式操作系统也是我的学习模板之一,但是我发现,无论是编程开…

    Linux 2023年6月6日
    094
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球