用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