mybatis-plus在使用多個mapper時報錯
報錯內容如下
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類纔行,就是要繼承的時候一定要指定泛型
報錯前我的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<A> {
}
--------------------------------------------------------------
//BService文件
public class BServiceImpl extends BaseServiceImpl {
}
public interface IBService extends IBaseService {
}
//Bmapper文件
@Mapper
public interface BMapper extends BaseMapper<B> {
}
----------------------------------------------------------
//公共類
public class BaseServiceImpl<T extends Object> extends ServiceImpl implements IBaseService<T> {
}
public interface IBaseService<T extends Object> {
}
修改成功後我的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<A> {
}
public interface IAService extends IBaseService<A> {
}
//Amapper文件
@Mapper
public interface AMapper extends BaseMapper<A> {
}
--------------------------------------------------------------
//BService文件
public class BServiceImpl extends BaseServiceImpl<B> {
}
public interface IBService extends IBaseService<B> {
}
//Bmapper文件
@Mapper
public interface BMapper extends BaseMapper<B> {
}
----------------------------------------------------------
//公共類
public class BaseServiceImpl<T extends Object> extends ServiceImpl implements IBaseService<T> {
}
public interface IBaseService<T extends Object> {
}
修改的點
1.MyApplication的MapperScan添加了com.baomidou.mybatisplus.core.mapper這個掃描範圍,這是mybatis-plus的BaseMapper所在的包位置
2.service繼承的時候指定對應的泛型,這樣是爲了能讓BaseMapper找到該映射的mapper,從而執行對應的mapper下的內置sql
你們沒有像我這樣公共類BaseServiceImpl這些,可以這樣寫
//AService文件
public class AServiceImpl extends ServiceImpl<AMapper ,A> {
}
public interface IAService extends IService<A> {
}
//Amapper文件
@Mapper
public interface AMapper extends BaseMapper<A> {
}
---------------------------------------------------------------------------------------------------------
//BService文件
public class BServiceImpl extends ServiceImpl<BMapper ,B> {
}
public interface IBService extends IService<B> {
}
//Bmapper文件
@Mapper
public interface BMapper extends BaseMapper<B> {
}