1. 程式人生 > >springboot整合mybatis遇到無法掃描MaperScan包的問題

springboot整合mybatis遇到無法掃描MaperScan包的問題

cti exceptio start leg inter 3.1 clip main app

1.啟動類加上@MaperScan註解後,一直報錯如下:

Error creating bean with name ‘platUserMapper‘ defined in file [D:\workspace\eclipse_data\vivo\target\classes\test\interf\domain\mapper\PlatUserMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required

原因是pom.xml沒有引入如下依賴,因為從SqlSessionDaoSupport 這個類的源碼中可以看出,原因是mybatis-spring-1.2.0中取消了自動註入SqlSessionFactory 和 SqlSessionTemplate

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>

2.引入如上依賴包後,再次報錯,報錯如下:

Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method ‘sqlSessionFactory‘ threw exception; nested exception is java.lang.NoSuchMethodError: org.apache.ibatis.session.Configuration.setVfsImpl(Ljava/lang/Class;)V

查看源碼發現該類中不存在set方法 ,升級mybatis版本如下:

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>

仍然報錯:Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method ‘sqlSessionFactory‘ threw exception; nested exception is java.lang.NoSuchMethodError: org.mybatis.spring.SqlSessionFactoryBean.setVfs(Ljava/lang/Class;)V

升級mybatis-spring:

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

更新pom後重新啟動,可正常啟動。

springboot整合mybatis遇到無法掃描MaperScan包的問題