做SpringBoot项目时报错:Application run failed;Error creating bean with name ‘userController’: Injection of resource dependencies failed
在网上搜了很多解决方案,最后艰难解决。总结下面几点
- controller、service、dao(mapper)、entity的类中缺少注解:@RestController、@Service、@Repository、@Table(name = “user”)(注意表名不要拼错)
- controller调用service、service调用dao时,没有使用注解@Resource
- SpringbootApplication.java中,类的声明前面没有使用注解@MapperScan(“com.jiang.dao”)扫描dao层的包
- Springboot版本和mybatis版本不一致。
我本人就是因为这个报错的,Springboot3以上版本对应的mybatis也要用3以上版本,我的Springboot是3.3.1,mybatis之前是1.3.2会报错,改成3.0.3就好了。具体对应的版本就需要你们自己去尝试了。 - 如果还有其他报错比如Factory method ‘sqlSessionFactory’ threw exception with message: Failed to parse mapping resource: 'file…
可能是mybatis的xml文件中出现拼写错误(本人也遇到了)。
大家可以看一下后续的详细报错,比如我后续还有Could not resolve type alias ‘com.example.entity.Admin’,就是我的resultType中类的路径拼写错了。
希望能帮到你们。