最近初学使用mybatis-plus
在自己手写generator并运行成功之后尝试在test中使用@Autowire注解引用接口
并练习insert操作结果遇到了
java.lang.IllegalStateException: Failed to load ApplicationContext
在错误信息中发现:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'addressServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.test.mapper.AddressMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.test.mapper.AddressMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
大概是无法注入Bean 导致的
检查生成器的Service类都有@Service注解,后依照参考的项目将所有的mapper类加上了@Repository与@Mapper注解,但问题依旧没有解决
后来在Test类头部加上了
@MapperScan("com.test.mapper")
如图:
至此问题解决
项目目录大致如下:
参考贴子:https://blog.csdn.net/zhangcc233/article/details/80898931