服务层继承ISevice引发的Consider marking one of the beans as @Primary错误解释

  • 简介
  • 错误信息
  • 错误原因
  • 解决办法

简介

今天写项目时想用lambdaQueryWrapper 时,想用链式操作,之后我的service服务层继承于IService,我的ServiceImpl同样继承于ServiceImpl,最后启动项目后发现出现了这样的报错

错误信息

Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

错误原因

根据发生这个错误后,经过我的筛查,发现MyBatis-plus 的IService.的ServiceImpl实现类中有一个 M baseMapper字段。

public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
    protected Log log = LogFactory.getLog(this.getClass());
    @Autowired
    protected M baseMapper;

报错的错误无非就是有两个Mapper存在,一个存在于ServiceImpl的baseMapper,一个我自己继承于BaseMapper的mapper。bean找不到了。

解决办法

通过@Primary注解,给我们的实体Mapper加上该注解就能解决BUG。

更多推荐

SpringBoot&Mybatis-Plus - 服务层Service继承ISevice引发的Consider marking one of the