baomidou / mybatis-plus

An powerful enhanced toolkit of MyBatis for simplify development
https://baomidou.com
Apache License 2.0
16.46k stars 4.31k forks source link

自己的 IBaseService 继承 Mybatis-Plus 提供的基类报updateById不存在 #5782

Closed qinfy closed 7 months ago

qinfy commented 1 year ago

当前使用版本(必填,否则不予处理)

3.5.1

该问题是如何引起的?(确定最新版也有问题再提!!!)

由3.1.0版本升级到3.5.1版本 3.3.3版本对com.baomidou.mybatisplus.extension.service.impl.ServiceImpl#sqlStatement方法做了过时处理导致的

重现步骤(如果有就写完整)

1、创建自己的IBaseMapper、IBaseService、 IBaseServiceImpl 继承 Mybatis-Plus 提供的基类 base mapper: public interface IBaseMapper<T> extends BaseMapper<T> {} base service: public interface IBaseService<T> extends IService<T> {} base serviceImpl: public class IBaseServiceImpl<M extends IBaseMapper<T>, T> extends ServiceImpl<IBaseMapper<T>, T> implements IBaseService<T> {} 业务类mapper、service、serviceImpl都继承自己的base mapper、base service、 base serviceImpl

2、业务类serviceImpl里调用批量更新方法updateBatchById 3.3.3之前都正常,升级3.3.3之后抛异常

报错信息

`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 cn.com.dhcc.platform.core.common.mapper.IBaseMapper.updateById

Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for cn.com.dhcc.platform.core.common.mapper.IBaseMapper.updateById

at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:96)
at com.baomidou.mybatisplus.extension.toolkit.SqlHelper.executeBatch(SqlHelper.java:192)
at com.baomidou.mybatisplus.extension.toolkit.SqlHelper.executeBatch(SqlHelper.java:217)
at com.baomidou.mybatisplus.extension.service.impl.ServiceImpl.executeBatch(ServiceImpl.java:240)
at com.baomidou.mybatisplus.extension.service.impl.ServiceImpl.updateBatchById(ServiceImpl.java:192)
at com.baomidou.mybatisplus.extension.service.IService.updateBatchById(IService.java:268)
at com.baomidou.mybatisplus.extension.service.IService$$FastClassBySpringCGLIB$$f8525d18.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:793)`

报错信息中的cn.com.dhcc.platform.core.common.mapper.IBaseMapper.updateById为自己的基类,继承了 Mybatis-Plus 提供的基类(第1条中的base mapper)

经排查是MybatisPlus对批量方法重新实现, com.baomidou.mybatisplus.extension.service.impl.ServiceImpl#updateBatchById 中的String sqlStatement = sqlStatement(SqlMethod.UPDATE_BY_ID);改为了String sqlStatement = getSqlStatement(SqlMethod.UPDATE_BY_ID); sqlStatement方法在3.3.3版本做了过时处理。 旧版本的sqlStatement和新版本的getSqlStatement方法类反射处理时有区别,不清楚这么做的原因。

在自己的IBaseServiceImpl 中按3.3.3之前的逻辑重写为遍历更新后错误解决,但无法使用新的批量处理逻辑

qinfy commented 1 year ago

image 重写为 image 后正常

startjava commented 1 year ago

你这两张图里的代码,哪里不一样?

VampireAchao commented 1 year ago
- getSqlStatement(SqlMethod.UPDATE_BY_ID)
+ sqlStatement(SqlMethod.UPDATE_BY_ID)
nieqiurong commented 12 months ago

提供你的工程样例过来看下.

stynaer commented 8 months ago

同样的问题,我不明白为何要将'sqlStatement'更改为'getSqlStatement'的逻辑。改回'sqlStatement'则业务恢复正常。

    @Override
    protected String getSqlStatement(SqlMethod sqlMethod) {
        return super.sqlStatement(sqlMethod);
    }