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

  • 相关阅读:
    android studio 中如何合并冲突(转)
    关于学习ZigBee的书籍
    多一点学习之外的人文思考
    有关技术文档的一点感想
    有关文学知识对我大学生活的影响
    【转】华为PCB布线规范
    【转】怎么样从一个疯狂下载者成为一个学习者!!!值得反省下的问题·~~
    时钟1
    关于有源滤波器和无源滤波器
    【转】zz个人的制板习惯流程
  • 原文地址:https://www.cnblogs.com/simple-flw/p/13911125.html
Copyright © 2011-2022 走看看