miemie 5 年之前
父節點
當前提交
55e5bc1a80

+ 22 - 4
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/TableInfo.java

@@ -144,6 +144,14 @@ public class TableInfo implements Constants {
     @Getter
     @Setter(AccessLevel.NONE)
     private boolean withUpdateFill;
+    /**
+     * 表字段是否启用了乐观锁
+     *
+     * @since 3.3.1
+     */
+    @Getter
+    @Setter(AccessLevel.NONE)
+    private boolean enableVersion;
 
     public TableInfo(Class<?> entityType) {
         this.entityType = entityType;
@@ -396,9 +404,19 @@ public class TableInfo implements Constants {
 
     void setFieldList(List<TableFieldInfo> fieldList) {
         this.fieldList = fieldList;
-        this.logicDelete = fieldList.parallelStream().anyMatch(TableFieldInfo::isLogicDelete);
-        this.withInsertFill = fieldList.parallelStream().anyMatch(TableFieldInfo::isWithInsertFill);
-        this.withUpdateFill = fieldList.parallelStream().anyMatch(TableFieldInfo::isWithUpdateFill);
+        fieldList.forEach(i -> {
+            if (i.isLogicDelete()) {
+                this.logicDelete = true;
+            }
+            if (i.isWithInsertFill()) {
+                this.withInsertFill = true;
+            }
+            if (i.isWithUpdateFill()) {
+                this.withUpdateFill = true;
+            }
+            if (i.isVersion()) {
+                this.enableVersion = true;
+            }
+        });
     }
-
 }

+ 4 - 1
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/OptimisticLockerInterceptor.java

@@ -107,6 +107,9 @@ public class OptimisticLockerInterceptor implements Interceptor {
                 String methodName = methodId.substring(methodId.lastIndexOf(StringPool.DOT) + 1);
                 Class<?> entityClass = et.getClass();
                 TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
+                if (tableInfo == null || !tableInfo.isEnableVersion()) {
+                    return invocation.proceed();
+                }
                 EntityField versionField = this.getVersionField(entityClass, tableInfo);
                 if (versionField == null) {
                     return invocation.proceed();
@@ -198,7 +201,7 @@ public class OptimisticLockerInterceptor implements Interceptor {
     public void setProperties(Properties properties) {
         // to do nothing
     }
-    
+
     private EntityField getVersionField(Class<?> parameterClass, TableInfo tableInfo) {
         return versionFieldCache.computeIfAbsent(parameterClass, mapping -> getVersionFieldRegular(parameterClass, tableInfo));
     }