mybatis-plus在使用多个mapper时报错

mybatis-plus在使用多个mapper时报错

  • 报错内容如下
    • 报错原因分析
    • 报错前我的Application,Service和Mapper文件如下
    • 修改成功后我的Application,Service和Mapper文件如下
    • 修改的点
    • 你们没有像我这样公共类BaseServiceImpl这些,可以这样写

报错内容如下

Field baseMapper in com.baomidou.mybatisplus.extension.service.impl.ServiceImpl required a single bean, but xxx were found:
- xxxAMapper: defined in file [D:\xxx\AMapper.class]
- xxxBMapper: defined in file [D:\xxx\BMapper.class]
- xxxCMapper: defined in file [D:\xxx\CMapper.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

Process finished with exit code 1

报错原因分析

1.很多service继承了com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
2.很多mapper继承了com.baomidou.mybatisplus.core.mapper.BaseMapper

service中使用getBaseMapper().insert(entity)等内置方法时候,会跳转到ServiceImpl中的getBaseMapper()方法(如下图所示)
报错点:
baseMapper不知道到底要获取哪一个继承他的mapper,到底要查哪个实体类的表,因为没有明确指明,就会报这个错。
修改思路:
继承IService和ServiceImpl的时候要能明确的指向对应的mapper类才行,就是要继承的时候一定要指定泛型

mybatis-plus在使用多个mapper时报错_第1张图片

报错前我的Application,Service和Mapper文件如下

@SpringBootApplication
@MapperScan(value={"cn.com.test.mapper"})
public class MyApplication {

	public static void main(String[] args) {
		SpringApplication.run(MyApplication.class, args);
	}

}


//AService文件
public class AServiceImpl extends BaseServiceImpl {

}

public interface IAService extends IBaseService {

}
//Amapper文件
@Mapper
public interface AMapper extends BaseMapper {

}
--------------------------------------------------------------
//BService文件
public class BServiceImpl extends BaseServiceImpl {

}

public interface IBService extends IBaseService {

}
//Bmapper文件
@Mapper
public interface BMapper extends BaseMapper {

}
----------------------------------------------------------
//公共类
public class BaseServiceImpl extends ServiceImpl implements IBaseService { 
 }
public interface IBaseService {
}

修改成功后我的Application,Service和Mapper文件如下

@SpringBootApplication
@MapperScan(value={"cn.com.test.mapper","com.baomidou.mybatisplus.core.mapper"})
public class MyApplication {

	public static void main(String[] args) {
		SpringApplication.run(MyApplication.class, args);
	}

}


//AService文件
public class AServiceImpl extends BaseServiceImpl {

}

public interface IAService extends IBaseService {

}
//Amapper文件
@Mapper
public interface AMapper extends BaseMapper {

}
--------------------------------------------------------------
//BService文件
public class BServiceImpl extends BaseServiceImpl {

}

public interface IBService extends IBaseService {

}
//Bmapper文件
@Mapper
public interface BMapper extends BaseMapper {

}
----------------------------------------------------------
//公共类
public class BaseServiceImpl extends ServiceImpl implements IBaseService { 
 }
public interface IBaseService {
}

修改的点

1.MyApplication的MapperScan添加了com.baomidou.mybatisplus.core.mapper这个扫描范围,这是mybatis-plus的BaseMapper所在的包位置
2.service继承的时候指定对应的泛型,这样是为了能让BaseMapper找到该映射的mapper,从而执行对应的mapper下的内置sql

你们没有像我这样公共类BaseServiceImpl这些,可以这样写

//AService文件
public class AServiceImpl extends ServiceImpl {

}

public interface IAService extends IService  {

}
//Amapper文件
@Mapper
public interface AMapper extends BaseMapper {

}
---------------------------------------------------------------------------------------------------------

//BService文件
public class BServiceImpl extends ServiceImpl {

}

public interface IBService extends IService {

}
//Bmapper文件
@Mapper
public interface BMapper extends BaseMapper {

}

你可能感兴趣的:(mybatisplus,mapper,mybatis-plus,java)