在学习使用 mybatis-plus 时,遇到一个奇怪的异常 如 代码一:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field baseMapper in com.baomidou.mybatisplus.extension.service.impl.ServiceImpl required a single bean, but 2 were found:
- sysUserMapper: defined in file [/Users/wzengheng/linewell/wis-mom-platform/wis-organization/wis-organization-microservice/target/classes/com/wis/organization/infrastructure/persistence/dao/SysUserMapper.class]
- sysUserRoleMapper: defined in file [/Users/wzengheng/linewell/wis-mom-platform/wis-organization/wis-organization-microservice/target/classes/com/wis/organization/infrastructure/persistence/dao/SysUserRoleMapper.class]
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
Disconnected from the target VM, address: '127.0.0.1:61021', transport: 'socket'
会出现这种问题的原因是在使用Mybatis-plus的时候 xxxService要实现IService接口,xxxxx要继承ServiceImpl<xxxxMapper, xxxx>, xxxxMapper 要继承BaseMapper接口,因此在进行注入的时候Spring区分不了要注入哪一个,因为每个mapper都要去继承BaseMapper。最简单的方法就是放弃使用Mybatis-plus 去使用Mybatis,这样是能正常使用的,但是这不是我想要的,所以下面是我解决这个问题的方法。
在碰到这种问题的时候网上也查找了很多的方法,但是都没有用,我是在实体类当中,使用工厂类来获取仓储的bean,我是这样子定义的
/**
*
*/
private static SysUserReference sysUserReference = RepoFactory.get(SysUserReference.class);
/**
*
*/
private static SysUserRoleReference sysUserRoleReference = RepoFactory.get(SysUserRoleReference.class);
定义公用的bean,这时候就会报这种错误,我将代码修改成下面这种要用到在去获取的方式 就能正常使用了,另外还有一个问题就是 实体类当中 不能使用@Autowired来进行注入,否则会报空指针异常。切记切记
/**
* 获取
*
* @return
*/
public Page<SysUser> selectList() {
return RepoFactory.get(SysReference.class).selectList(this.obtainPage(), this);
}