话不多说先上图,这是启动类的配置,这里配置了@ComponentScan("我的mapper的接口")
接下来是我的项目结构截图
然后是service 的截图,我在这里加了注解@Service
最后我在测试类里面 的截图
最后附上我的maven的pom 我开始怀疑是这个配置的有问题了
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.2.RELEASE
com.example
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot
1.8
org.projectlombok
lombok
1.16.10
org.springframework.boot
spring-boot-starter-cache
org.springframework.boot
spring-boot-starter-data-redis
io.lettuce
lettuce-core
redis.clients
jedis
com.alibaba
druid
1.1.10
org.springframework.boot
spring-boot-starter-security
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.1
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
io.projectreactor
reactor-test
test
junit
junit
test
org.springframework.boot
spring-boot-maven-plugin
src/main/java
**/*.xml
src/main/resources
详细的报错信息如下:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.service.HrService' 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.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1695) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1253) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
经过几位大佬的提醒,在启动类新增了@MapperScan("com.example.Mappar"),同时在测试类加入启动类@RunWith(SpringRunner.class) 解决了找不dao bean 的问题,但随后又产生了一个新的报错如下:
与之对应的我的mapper.xml文件如下:
我的mapper接口如下:
最后我的HrService:
详细报错如下:
我在网上翻阅了资料:有的解决办法是看pom.xml里面的build 下 resouce 没有添加,我添加了后发现,还是无效。此外在namespace 和对应的service 里面,mapper 方法的名称都是一致,在torget目录下,我的HrMappar.class 和HrService.xml在同一目录下。很疑惑问题出在哪里了。