当前使用版本(必填,否则不予处理)
当前使用的版本是 3.4.3.4,是从 3.4.1 升级上来的。
该问题是如何引起的?(确定最新版也有问题再提!!!)
在升级版本后,对项目原功能进行测试时发现:
- 新增单条数据(
save())时没有问题; - 批量新增(
saveBatch())时会报此错误:Mapped Statements collection does not contain value for com.baomidou.mybatisplus.core.mapper.BaseMapper.insert;
相关 Issues
- https://github.com/baomidou/mybatis-plus/issues/3382
- https://github.com/baomidou/mybatis-plus/issues/3644
- https://gitee.com/baomidou/mybatis-plus/issues/I44D1K
代码结构
// 1.具体的业务服务实现类
@Service
public class UmsAdminRoleRelationServiceImpl extends BasicServiceImpl<UmsAdminRoleRelationMapper, UmsAdminRoleRelation> implements IUmsAdminRoleRelationService {
// ...
}
// 2.这里使用了自定义扩展的 BaseMapper
public class BasicServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<BaseMapper<T>, T> implements IBasicService<T> {}
// 3.调用
umsAdminRoleRelationService.saveBatch(insertAdminRoleList);
报错信息
### Error updating database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.baomidou.mybatisplus.core.mapper.BaseMapper.insert
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.baomidou.mybatisplus.core.mapper.BaseMapper.insert
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.baomidou.mybatisplus.core.mapper.BaseMapper.insert
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.baomidou.mybatisplus.core.mapper.BaseMapper.insert
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:96)
at com.baomidou.mybatisplus.extension.toolkit.SqlHelper.executeBatch(SqlHelper.java:188)
at com.baomidou.mybatisplus.extension.toolkit.SqlHelper.executeBatch(SqlHelper.java:213)
at com.baomidou.mybatisplus.extension.service.impl.ServiceImpl.executeBatch(ServiceImpl.java:240)
at com.baomidou.mybatisplus.extension.service.impl.ServiceImpl.saveBatch(ServiceImpl.java:136)
at com.baomidou.mybatisplus.extension.service.IService.saveBatch(IService.java:73)
Comment From: gfppower
我刚好遇到 ,顺便解决了一下。首先贴上我的解决方案,再贴原因: 新建一个继承自serviceImpl的service,重写如下方法。然后修改你的service实现继承这个新的service. /* * 修复由于继承 mapper导致的 saveBatch存储失败的问题 * @param sqlMethod * @return / @Override protected String getSqlStatement(SqlMethod sqlMethod) { Class<?> clazz = mapperClass ; if( ObjectUtil.isNotNull( this.baseMapper ) && ObjectUtil.isNotNull( this.baseMapper.getClass() ) ){ clazz = this.baseMapper.getClass().getInterfaces()[0] ; } return SqlHelper.getSqlStatement(clazz, sqlMethod); }
下面贴原因。我这块遇到是问题是,我在定义service时使用的mapper并不是在代码中实际使用的mapper,比如UserMapper,仅仅是一个接口而已。而实际使用的mapper时实现这个接口的另一个接口,比如AdminUserMapper extends UserMapper。 在程序调用过程中,由于mybatis的机制。其中获得内置方法列表中,并无UserMapper相关的方法。而只有AdminUserMapper相关的方法。而mybatis-plus在使用时。采用的这个方法仅仅能返回UserMapper的class,所以组织后续获取对应的方法时出现异常。在MybatisConfiguration的StrictMap的public V get(Object key)方法的的问题。可以追踪一下。有更好解决办法的英雄接的告诉我。