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
图片描述

阅读 7k
3 个回答

你的启动类加上@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最佳

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题