1. 程式人生 > >spring boot 因mybatis版本引發的問題

spring boot 因mybatis版本引發的問題

使用spring boot整合專案的時候,遇到了一個異常,如下:

2018-06-03 20:44:17.660  WARN 301924 --- 
[           main] o.s.b.f.s.DefaultListableBeanFactory     :
Bean creation exception on non-lazy FactoryBean type check: org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'roleDao' defined in
file [B:\workspace_spring\resume\baseservice\target\classes\com\ma\base\dao\RoleDao.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class
]: org.springframework.beans.BeanInstantiationException: Factory method 'sqlSessionFactory' threw exception; nested exception is java.lang.NoSuchMethodError: org.mybatis.spring.SqlSessionFactoryBean.setVfs(Ljava/lang/Class;)V

根據異常資訊,MybatisAutoConfiguration類中出現了方法呼叫異常,開啟原始碼檢視

  @Bean
  @ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); factory.setDataSource(dataSource); factory.setVfs(SpringBootVFS.class);

發現 factory.setVfs(SpringBootVFS.class)在SqlSessionFactoryBean類中不存在,導致sqlsesessionFactory建立失敗。糾結了很久,找了很多資料,檢視spring boot版本,又看了mybatis-spring-boot-starter整合包的版本,都沒有問題的,為什麼我的專案啟動就遇到了這種問題呢!!!無奈之下,對比了之前的專案和現在專案結構的maven引入jar包結構,我坑。。。結果居然是在父工程中定義了這個依賴

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.2.2</version>
</dependency>

坑爹…原來就是在父工程中這裡搞得鬼,去掉後,在此檢視maven引入的版本mybatis-spring-1.3.1,OK。萬事大吉,啟動正常。