zoukankan      html  css  js  c++  java
  • springBoot整合Mybatis为什么可以省略mybatis-config.xml

    springboot整合Mybatis为什么可以省略mybatis-config.xml

    ​ 原来我们在使用mybatis的时候都是要配置mybatis-config.xml,但是用springboot整合Mybatis只要很简单的配置就可以了。

    why

    1:原来的mybatis-config.xml中的配置信息很多都整合到了MybatisProperties中去了。

    @ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX)
    public class MybatisProperties {
    
      public static final String MYBATIS_PREFIX = "mybatis";
    
      private static final ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
    //主配置文件位置(mybatis-config.xml),也是可以不配置的
      private String configLocation;
     //mapper.xml文件位置
      private String[] mapperLocations;
    
      private String typeAliasesPackage;
    
      private Class<?> typeAliasesSuperType;
    
      private String typeHandlersPackage;
    
      private boolean checkConfigLocation = false;
    
      private ExecutorType executorType;
    
    

    这些属性都是可以在springboot的配置文件中配置的。


    当configLocation没有不配也是可以的

    ​ 当configLocation属性配置为null的时候,我们可以看下SqlSessionFactoryBean这个类,这个类实现了FactoryBean通过afterPropertiesSet自定义实例化bean。

    } else if (this.configLocation != null) {
          xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null, this.configurationProperties);
          targetConfiguration = xmlConfigBuilder.getConfiguration();
        } else {
          LOGGER.debug(
              () -> "Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration");
          targetConfiguration = new Configuration();
          Optional.ofNullable(this.configurationProperties).ifPresent(targetConfiguration::setVariables);
        }
    

    可以看到在配置了配置文件会通过xmlConfigBuilder进行解析,获得得configuration对象

    ,当没有配置文件的时候,可以看到打印的debug日志,

    Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration

    就new 一个Configuration,然后使用默认配置。

    That all

  • 相关阅读:
    第五章 运输层(UDP和TCP三次握手,四次挥手分析)
    Fluent Ribbon 第六步 StartScreen
    Fluent Ribbon 第七步 状态栏
    Fluent Ribbon 第八步 其他控件
    Avalondock 第四步 边缘停靠
    node.js开发学习一HelloWorld
    Winform应用程序实现通用遮罩层
    输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的
    Navicat连接MySQL8+时出现2059报错
    win10安装MySql教程
  • 原文地址:https://www.cnblogs.com/simple-flw/p/13911125.html
Copyright © 2011-2022 走看看