【Java】IDEA maven工程spring boot整合mybatis失败
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.pwq.www.myProject.user.service.StudentService com.pwq.www.myProject.user.controller.StudentController.studentService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.pwq.www.myProject.user.mapper.StudentMapper com.pwq.www.myProject.user.service.StudentService.studentMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.pwq.www.myProject.user.mapper.StudentMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
爆了那么老长的错,大概是我依赖注入的mapper有问题,但是不清楚哪边错了
mapper.xml
mapper
service
controller
回答
你的启动类加上@MapperScan(basePackages = {"mapper类所在的包名"})
mapper类不需要加@Repository注解
Mapper 类应该加上 @Mapper 而不是 @Repository。
=============================正确答案==========================================
有2种方式:1.启动类上加@MapperScan(basePackages = {"mapper类所在的包名"})
2.Mapper 类应该加上 @Mapper 而不是 @Repository。
另外mybatis的配置要注意,.xml的扫描有问题会报Invalid bound statement (not found)错误
.xml文件放到recoures最佳
以上是 【Java】IDEA maven工程spring boot整合mybatis失败 的全部内容, 来源链接: utcz.com/a/87913.html