注解

@MapperScan

Springboot 的依赖注入需要通过  @ComponentScan  来扫描所配置的 bean,但是该组件会忽略抽象类和接口,因此 Mybatis 的 mapper 接口就需要使用其专门为 Springboot 依赖注入提供的  @MapperScan  扫描器。
通常配置扫描基础包(可以设多个)。

1
@MapperScan(basePackages = "com.example.dao.mapper")

若不配置,则会出现 bean 注入失败的问题。

1
2
org.springframework.beans.factory.UnsatisfiedDependencyException
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.dao.mapper.xxxMapper' 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.annotation.Qualifier(value=personMapper)}

评论