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
解析原因是因为框架就是不允许这个样子, 因为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(); }
-
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(); }
具体还是使用yml优雅一点,可能没人会两个都用