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> {

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章