在MyBatis中mapper接口的bean注入问题
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'regionWareController': Unsatisfied dependency expressed through field 'regionWareService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'regionWareServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.atguigu.ssyx.sys.mapper.RegionWareMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1420) ~[spring-beans-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.11.RELEASE.jar:5.2.11.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.11.RELEASE.jar:5.2.11.RELEASE]
mybatis中mapper接口bean注入不能通过传统的@Repository或者@Component写在mapper接口上,有两种办法解决:
1.在mapper接口上加上@Mapper(这个注解由mybatis提供)
@Mapper
public interface RegionMapper extends BaseMapper<Region> {
}
2.SpringBoot在启动类加包扫描注解:@MapperSacn
@SpringBootApplication
@MapperScan("com.atguigu.ssyx.sys.mapper")
public class ServiceSysApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceSysApplication.class, args);
}
}