• Mybatis || Mybatis-Plus中configuration和configLocation无法同时使用记录


    Mybatis || Mybatis-Plus中configuration和configLocation无法同时使用记录

    1. 当在SpringBoot项目中配置的yml中对于mybatis || mp的参数同时配置了configuration和configLocation的情况下会导致的问题如下

    Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is java.lang.IllegalStateException: Property 'configuration' and 'configLocation' can not specified with together
    
    • 1

    解析原因是因为框架就是不允许这个样子, 因为mp是mybatis的加强, 所以也会出现同样的问题

    2. 具体代码如下

    • mybatis的代码是SqlSessionFactoryBean类中的

      @Override
      public void afterPropertiesSet() throws Exception {
          notNull(dataSource, "Property 'dataSource' is required");
          notNull(sqlSessionFactoryBuilder, "Property 'sqlSessionFactoryBuilder' is required");
          state((configuration == null && configLocation == null) || !(configuration != null && configLocation != null),
                "Property 'configuration' and 'configLocation' can not specified with together");
      
          this.sqlSessionFactory = buildSqlSessionFactory();
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    • mybatis的代码是MybatisSqlSessionFactoryBean类中

      @Override
      public void afterPropertiesSet() throws Exception {
          notNull(dataSource, "Property 'dataSource' is required");
          state((configuration == null && configLocation == null) || !(configuration != null && configLocation != null),
                "Property 'configuration' and 'configLocation' can not specified with together");
          //TODO 清理掉资源  建议不要保留这个玩意了
          SqlRunner.DEFAULT.close();
          this.sqlSessionFactory = buildSqlSessionFactory();
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9

    具体还是使用yml优雅一点,可能没人会两个都用

  • 相关阅读:
    Vue 源码解读(7)—— Hook Event
    总结File类的用法及InputStream、OutputStream的用法
    以太网帧格式 以及 认识MAC地址
    Spring框架----AOP全集
    RabbitMQ系列【18】对象序列化机制
    【单片机学习笔记】Windows+Vscode+STM32F4+freeRTOS+FatFs gcc环境搭建
    React面试:谈谈虚拟DOM,Diff算法与Key机制
    配电房轨道式巡检机器人方案
    什么是浅拷贝和深拷贝,如何用 js 代码实现?
    【微服务保护】
  • 原文地址:https://blog.csdn.net/weixin_43194885/article/details/127720826