报错内容如下:
Field userService in com.zhi.api.test05.controller.UserController required a bean of type 'com.zhi.api.test05.service.UserService' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
检查后发现主类没有设置包扫描,加上MapperScan如图:
重新执行,报错如下:
java: 无法访问org.mybatis.spring.annotation.MapperScan
错误的类文件: /D:/apache-maven-3.3.9/repository/org/mybatis/mybatis-spring/3.0.3/mybatis-spring-3.0.3.jar!/org/mybatis/spring/annotation/MapperScan.class
类文件具有错误的版本 61.0, 应为 52.0
请删除该文件或确保该文件位于正确的类路径子目录中。
版本问题,查询资料后,发现是mybatis-spring的版本问题。清除缓存后启动即可成功。
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<!-- <version>3.0.3</version> -->
<version>2.0.6</version>
</dependency>
这里参考和两个博客的解决思路,如下: