org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ProductionController': Unsatisfied dependency expressed through field 'ProductionService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ProductionServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'xxx.xx.demo.mapper.ProductionMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
报错:
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“ProductionController”的bean时出错:通过字段“ProductionService”表示不满意的依赖;org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“ProductionServiceImpl”的bean时出错:不满意的依赖关系通过字段“baseMapper”表示;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为'xxx.xx.demo.mapper.ProductionMapper'的合格bean可用:期望至少有一个bean符合自动候选资格。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
解决方案:
springboot启动类上加上@MapperScan(包路径)
解析:
通过在 Spring Boot 项目中使用 @MapperScan 注解,可以将 MyBatis 的 Mapper 接口自动注册到 Spring 容器中,在业务代码中直接注入和使用这些 Mapper 接口,而无需手动实例化。这样可以方便地使用 MyBatis 进行数据库操作。