我有一个SpringBoot 2.0.4.RELEASE项目,使用
mybatis-spring-boot-autoconfigure-1.3.2.jar
,并且工作正常。
mybatis-spring-boot-autoconfigure-1.3.2.jar
将创建类型为 [com.zaxxer.hikari.HikariDataSource] 的 bean datasource
。
但是当我使用
jar xvf
提取 jar
文件时,然后我使用 jar cvmf0 /META-INF/MANIFEST.MF
来恢复 jar。虽然 jar 是相同的,但它无法创建 datasource
bean。java -Ddebug -jar
来运行我的项目,发现spring-boot-2.0.4.RELEASE.jar
不在类路径的顶部。MybatisAutoConfiguration:
Did not match:
- @ConditionalOnBean (types: javax.sql.DataSource; SearchStrategy: all) **did not find any beans of typejavax.sql.DataSource** (OnBeanCondition)
Matched:
- @ConditionalOnClass found required classes 'org.apache.ibatis.session.SqlSessionFactory','org.mybatis.spring.SqlSessionFactoryBean'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
Caused by: java.lang.IllegalArgumentException: Property'sqlSessionFactory' or 'sqlSessionTemplate' are required
at org.springframework.util.Assert.notNull(Assert.java:193) ~[spring-core-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
at org.mybatis.spring.support.SqlSessionDaoSupport.checkDaoConfig(SqlSessionDaoSupport.java:74)~[mybatis-spring-1.3.2.jar!/:1.3.2]
at org.mybatis.spring.mapper.MapperFactoryBean.checkDaoConfig(MapperFactoryBean.java:73) ~[mybatis-spring-1.3.2.jar!/:1.3.2]
at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44~[spring-tx-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1758)~[spring-beans-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1695)~[spring-beans-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
... 108 common frames omitted
application.yml 是:
spring:
profiles:
active: prod
datasource:
name: dataSource
url: xxx
username: xxx
password: xxx
#type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: XXXX
mybatis:
typeAliasesPackage: xxx
mapperLocations: classpath:mybatis/mapper/*Mapper.xml
我认为你应该仔细检查 application.yml,尤其是
driver-class-name
MyBatis-Spring-Boot-Starter 将:
DataSource
SqlSessionFactory
的实例,并使用 DataSource
将
SqlSessionFactoryBean
SqlSessionTemplate
中取出的
SqlSessionFactory
SqlSessionTemplate
并将它们注册到Spring上下文,以便它们可以注入到您的bean中简单来说,mybatis-spring-boot-starter会自动在属性中找到你的dataSource配置,并生成执行SQL命令所需的sqlSessionFactory。使用mybatis-spring,你不需要手动管理提交和回滚,Spring会帮你管理它们,你只需要专注于Coding。
注意:如果Spring在properties或yml中找不到任何dataSource配置,那么Spring将不会生成任何类(例如SqlSessionFactory)。