MapperScan和ComponentScan同时使用问题

@MapperScan:
1.首先了解@Mapper
    在接口上添加了@Mapper,在编译以后就会生成相应的接口实现类。
    不过须要在每一个接口上面进行配置,为了简化开发,就有了 @MapperScan。spring

@MapperScan:apache

指定要变成实现类的接口所在的包,而后包下面的全部接口在编译以后都会生成相应的实现类。mybatis

@ComponentScan:
会自动扫描包路径下面的全部@Controller、@Service、@Repository、@Component 的类,并把符合扫描规则的类装配到spring容器中。app

@MapperScan和@ComponentScan能够同时使用。spa


若是都是扫描的相同路径时,对于同一个接口,可能就会出现识别错误。好比设计

在springBoot项目的Application上面定义了code

@MapperScan(basePackages = { "com" }) @SpringBootApplication
@SpringBootApplication包含了@ComponentScan。
在此项目下面有一个接口是com.xxInf,实现是
com.xxImpl在实现类上面经过@Service加入spring容器中
咱们在注入的时候接口时
 @Autowired private com.xxInf xx;

可能识别的不是xxImpl,而去mybatis里面经过反射找绑定,这样就会出现BindingException错误blog

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

因此在设计项目结构的时候要把mapper放到一个合适的位置,经过设置MapperScan的路径basePackages 好避免这种冲突接口

相关文章
相关标签/搜索