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

}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值