用MybatisPlus代码生成器之后,运行springboot项目报错:UnsatisfiedDependencyException: Error creating bean with name
用MybatisPlus代码生成器生成代码之后,运行springboot项目报错:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘userController’: Unsatisfied dependency expressed through field ‘userService’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘userServiceImpl’: Unsatisfied dependency expressed through field ‘baseMapper’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.tang.code.mapper.UserMapper’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations:
错误截图如下:
一、解决方法
解决方法:在Mapper接口上查看是否存在Mapper注解,如果没有就加上。或者在SpringBoot启动类上加上MapperScan扫描注解。或者在MybatisPlus的配置类上加MapperScan扫描注解。注意后面两种方法要将Mapper接口组件注入到spring容器中,虽然不会报错,但是会爆红。如下:
或
或
二、原因分析
用MybatisPlus代码生成插件生成的Mapper接口代码,默认不会加Mapper注解,所以spring容器找不到该组件,就会报错UnsatisfiedDependencyException。
如果不是这里有问题的话,可以去看这篇文章:
https://blog.csdn.net/qq_44860722/article/details/122586661