转自:https://www.cnblogs.com/mkl34367803/p/11218082.html
今天闲来无事,学习springboot整合mybatis,在bilibili看视频学的,视频中在dao层的interface上面加上org.apache.ibatis.annotations.Mapper注解就可以了,但是我一直报错。这个应该是springboot整合mybatis的一个bug(新版的)
Description:
Field userDao in com.yuanqiao.service.impl.UserServiceImpl required a bean of type 'com.yuanqiao.dao.UserDao' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
第一个报错的解决方式就是org.mybatis.spring.annotation.MapperScan注解
可以看到报错已经变了,说明dao层的接口已经委托给spring管理了,但是报错变成了:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in file [H:\WorkSpace\idea20170307\swaggerdemo\target\classes\com\yuanqiao\dao\UserDao.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
这个报错的原因是我之前引入oracle的坐标的时候,有报错,因为oracle不是免费的,所以在@SpringBootApplication注解后面加了一个排除数据源配置的属性,@SpringBootApplication(exclude = DataSourceAutoConfiguration.class),到这里大家应该都知道问题的所在了吧。
那就是Mybatis本生就是用来管理数据源的一个东西,exclude = DataSourceAutoConfiguration.class加上后,mybatis的自动装配就没法完成,所以就报错:
Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required