Kaynağa Gözat

轻微优化一下乐观锁

miemie 5 yıl önce
ebeveyn
işleme
33897a8da6

+ 22 - 30
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisDefaultParameterHandler.java

@@ -19,11 +19,13 @@ import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
 import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
-import com.baomidou.mybatisplus.core.toolkit.*;
+import com.baomidou.mybatisplus.core.toolkit.Constants;
+import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
+import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import org.apache.ibatis.executor.ErrorContext;
 import org.apache.ibatis.mapping.*;
 import org.apache.ibatis.reflection.MetaObject;
-import org.apache.ibatis.reflection.Reflector;
 import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
 import org.apache.ibatis.session.Configuration;
 import org.apache.ibatis.type.JdbcType;
@@ -85,26 +87,16 @@ public class MybatisDefaultParameterHandler extends DefaultParameterHandler {
         return parameterObject;
     }
 
-    @SuppressWarnings("unchecked")
     private static void process(MappedStatement ms, Object parameterObject) {
         TableInfo tableInfo = null;
         Object entity = parameterObject;
-        Map realEtMap = null;
         if (parameterObject instanceof Map) {
             Map<?, ?> map = (Map<?, ?>) parameterObject;
             if (map.containsKey(Constants.ENTITY)) {
                 Object et = map.get(Constants.ENTITY);
                 if (et != null) {
-                    if (et instanceof Map) {
-                        realEtMap = (Map) et;
-                        if (realEtMap.containsKey(Constants.MP_OPTLOCK_ET_ORIGINAL)) {
-                            entity = realEtMap.get(Constants.MP_OPTLOCK_ET_ORIGINAL);
-                            tableInfo = TableInfoHelper.getTableInfo(entity.getClass());
-                        }
-                    } else {
-                        entity = et;
-                        tableInfo = TableInfoHelper.getTableInfo(entity.getClass());
-                    }
+                    entity = et;
+                    tableInfo = TableInfoHelper.getTableInfo(entity.getClass());
                 }
             }
         } else {
@@ -119,22 +111,22 @@ public class MybatisDefaultParameterHandler extends DefaultParameterHandler {
             } else {
                 updateFill(metaObject, tableInfo);
                 //覆盖乐观锁里面的字段
-                if (tableInfo.isEnableVersion() && realEtMap != null) {
-                    Reflector forClass = metaObject.getReflectorFactory().findForClass(entity.getClass());
-                    for (String property : forClass.getGetablePropertyNames()) {
-                        try {
-                            //过滤掉乐观锁属性
-                            if (!realEtMap.get(Constants.MP_OPTLOCK_VERSION_COLUMN).equals(property)) {
-                                Object value = forClass.getGetInvoker(property).invoke(entity, new Object[]{});
-                                if (value != null) {
-                                    realEtMap.put(property, value);
-                                }
-                            }
-                        } catch (ReflectiveOperationException e) {
-                            throw ExceptionUtils.mpe(e);
-                        }
-                    }
-                }
+//                if (tableInfo.isEnableVersion() && realEtMap != null) {
+//                    Reflector forClass = metaObject.getReflectorFactory().findForClass(entity.getClass());
+//                    for (String property : forClass.getGetablePropertyNames()) {
+//                        try {
+//                            //过滤掉乐观锁属性
+//                            if (!realEtMap.get(Constants.MP_OPTLOCK_VERSION_COLUMN).equals(property)) {
+//                                Object value = forClass.getGetInvoker(property).invoke(entity, new Object[]{});
+//                                if (value != null) {
+//                                    realEtMap.put(property, value);
+//                                }
+//                            }
+//                        } catch (ReflectiveOperationException e) {
+//                            throw ExceptionUtils.mpe(e);
+//                        }
+//                    }
+//                }
             }
         }
     }

+ 9 - 6
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractMethod.java

@@ -236,11 +236,14 @@ public abstract class AbstractMethod implements Constants {
         return infoStream.map(function).collect(joining(joiningVal));
     }
 
-    protected String optlockVersion() {
-        return "<if test=\"et instanceof java.util.Map\">" +
-            " AND ${et." + Constants.MP_OPTLOCK_VERSION_COLUMN +
-            "}=#{et." + Constants.MP_OPTLOCK_VERSION_ORIGINAL + StringPool.RIGHT_BRACE +
-            "</if>";
+    protected String optlockVersion(TableInfo tableInfo) {
+        if (tableInfo.isEnableVersion()) {
+            return "<if test=\"oli != null\">" +
+                " AND ${oli." + Constants.MP_OPTLOCK_VERSION_COLUMN +
+                "}=#{oli." + Constants.MP_OPTLOCK_VERSION_ORIGINAL + StringPool.RIGHT_BRACE +
+                "</if>";
+        }
+        return EMPTY;
     }
 
     /**
@@ -332,8 +335,8 @@ public abstract class AbstractMethod implements Constants {
      * 获取自定义方法名,未设置采用默认方法名
      * https://gitee.com/baomidou/mybatis-plus/pulls/88
      *
-     * @author 义陆无忧
      * @return method
+     * @author 义陆无忧
      */
     public String getMethod(SqlMethod sqlMethod) {
         return sqlMethod.getMethod();

+ 1 - 1
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/UpdateById.java

@@ -33,7 +33,7 @@ public class UpdateById extends AbstractMethod {
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         boolean logicDelete = tableInfo.isLogicDelete();
         SqlMethod sqlMethod = SqlMethod.UPDATE_BY_ID;
-        final String additional = optlockVersion() + tableInfo.getLogicDeleteSql(true, true);
+        final String additional = optlockVersion(tableInfo) + tableInfo.getLogicDeleteSql(true, true);
         String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(),
             sqlSet(logicDelete, false, tableInfo, false, ENTITY, ENTITY_DOT),
             tableInfo.getKeyColumn(), ENTITY_DOT + tableInfo.getKeyProperty(), additional);

+ 1 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/Constants.java

@@ -112,6 +112,7 @@ public interface Constants extends StringPool {
     /**
      * 乐观锁字段
      */
+    String MP_OPTLOCK_INTERCEPTOR = "oli";
     String MP_OPTLOCK_VERSION_ORIGINAL = "MP_OPTLOCK_VERSION_ORIGINAL";
     String MP_OPTLOCK_VERSION_COLUMN = "MP_OPTLOCK_VERSION_COLUMN";
     String MP_OPTLOCK_ET_ORIGINAL = "MP_OPTLOCK_ET_ORIGINAL";

+ 1 - 1
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/AlwaysUpdateSomeColumnById.java

@@ -58,7 +58,7 @@ public class AlwaysUpdateSomeColumnById extends AbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.UPDATE_BY_ID;
-        final String additional = optlockVersion() + tableInfo.getLogicDeleteSql(true, true);
+        final String additional = optlockVersion(tableInfo) + tableInfo.getLogicDeleteSql(true, true);
         String sqlSet = this.filterTableFieldInfo(tableInfo.getFieldList(), getPredicate(),
             i -> i.getSqlSet(true, ENTITY_DOT), NEWLINE);
         sqlSet = SqlScriptUtils.convertSet(sqlSet);

+ 3 - 9
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/OptimisticLockerInterceptor.java

@@ -113,19 +113,13 @@ public class OptimisticLockerInterceptor implements Interceptor {
                     return invocation.proceed();
                 } else {
                     List<EntityField> fields = entityFieldsCache.computeIfAbsent(entityClass, this::getFieldsFromClazz);
-                    Map<String, Object> entityMap = new HashMap<>(fields.size());
-                    for (EntityField ef : fields) {
-                        Field fd = ef.getField();
-                        entityMap.put(fd.getName(), fd.get(et));
-                    }
+                    Map<String, Object> entityMap = new HashMap<>(3);
                     String versionColumnName = versionField.getColumnName();
                     //update to cache
                     versionField.setColumnName(versionColumnName);
-                    entityMap.put(field.getName(), updatedVersionVal);
-                    entityMap.put(Constants.MP_OPTLOCK_VERSION_ORIGINAL, originalVersionVal);
                     entityMap.put(Constants.MP_OPTLOCK_VERSION_COLUMN, versionColumnName);
-                    entityMap.put(Constants.MP_OPTLOCK_ET_ORIGINAL, et);
-                    map.put(Constants.ENTITY, entityMap);
+                    entityMap.put(Constants.MP_OPTLOCK_VERSION_ORIGINAL, originalVersionVal);
+                    map.put(Constants.MP_OPTLOCK_INTERCEPTOR, entityMap);
                     Object resultObj = invocation.proceed();
                     if (resultObj instanceof Integer) {
                         Integer effRow = (Integer) resultObj;