Ver Fonte

修复在极端情况下saveOrUpdate执行错误

Caratacus há 6 anos atrás
pai
commit
55bf6278a7

+ 2 - 9
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/activerecord/Model.java

@@ -19,6 +19,7 @@ import java.io.Serializable;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
 import org.apache.ibatis.session.SqlSession;
 import org.mybatis.spring.SqlSessionUtils;
@@ -72,15 +73,7 @@ public abstract class Model<T extends Model> implements Serializable {
      */
     @Transactional(rollbackFor = Exception.class)
     public boolean insertOrUpdate() {
-        if (StringUtils.checkValNull(pkVal())) {
-            // insert
-            return insert();
-        } else {
-            /*
-             * 更新成功直接返回,失败执行插入逻辑
-             */
-            return updateById() || insert();
-        }
+        return StringUtils.checkValNull(pkVal()) || Objects.isNull(selectById(pkVal())) ? insert() : updateById();
     }
 
     /**

+ 6 - 15
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/service/impl/ServiceImpl.java

@@ -41,8 +41,8 @@ import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
 import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
-import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
 
 /**
  * <p>
@@ -149,14 +149,7 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
             TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
             if (null != tableInfo && StringUtils.isNotEmpty(tableInfo.getKeyProperty())) {
                 Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty());
-                if (StringUtils.checkValNull(idVal)) {
-                    return save(entity);
-                } else {
-                    /*
-                     * 更新成功直接返回,失败执行插入逻辑
-                     */
-                    return updateById(entity) || save(entity);
-                }
+                return StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal)) ? save(entity) : updateById(entity);
             } else {
                 throw ExceptionUtils.mpe("Error:  Can not execute. Could not find @TableId.");
             }
@@ -181,16 +174,14 @@ public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {
                 }
                 if (null != tableInfo && StringUtils.isNotEmpty(tableInfo.getKeyProperty())) {
                     Object idVal = ReflectionKit.getMethodValue(cls, anEntityList, tableInfo.getKeyProperty());
-                    if (StringUtils.checkValNull(idVal)) {
-                        String sqlStatement = sqlStatement(SqlMethod.INSERT_ONE);
-                        batchSqlSession.insert(sqlStatement, anEntityList);
+                    if (StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal))) {
+                        batchSqlSession.insert(sqlStatement(SqlMethod.INSERT_ONE), anEntityList);
                     } else {
-                        String sqlStatement = sqlStatement(SqlMethod.UPDATE_BY_ID);
                         MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>();
                         param.put(Constants.ENTITY, anEntityList);
-                        batchSqlSession.update(sqlStatement, param);
-                        //不知道以后会不会有人说更新失败了还要执行插入 😂😂😂
+                        batchSqlSession.update(sqlStatement(SqlMethod.UPDATE_BY_ID), param);
                     }
+                    //不知道以后会不会有人说更新失败了还要执行插入 😂😂😂
                     if (i >= 1 && i % batchSize == 0) {
                         batchSqlSession.flushStatements();
                     }