Преглед изворни кода

优化数据库操作增强处理逻辑

woody пре 1 година
родитељ
комит
0288d0e7bb

+ 6 - 4
framework-database/src/main/java/com/chelvc/framework/database/interceptor/PropertyUpdateInterceptor.java

@@ -177,10 +177,11 @@ public class PropertyUpdateInterceptor implements Interceptor {
         }
 
         // 设置创建人字段值
-        boolean automaticCreator = !this.isContains(insert.getColumns(), Expressions.CREATOR_COLUMN);
+        Expression operator = this.operator();
+        boolean automaticCreator = operator != Expressions.NULL_VALUE
+                && !this.isContains(insert.getColumns(), Expressions.CREATOR_COLUMN);
         if (automaticCreator) {
             insert.addColumns(Expressions.CREATOR_COLUMN);
-            Expression operator = this.operator();
             ItemsList items = insert.getItemsList();
             if (items instanceof MultiExpressionList) {
                 ((MultiExpressionList) items).getExpressionLists().forEach(el -> el.getExpressions().add(operator));
@@ -217,10 +218,11 @@ public class PropertyUpdateInterceptor implements Interceptor {
         }
 
         // 设置更新人字段值
-        boolean automaticUpdater = !this.isContains(insert.getColumns(), Expressions.UPDATER_COLUMN);
+        Expression operator = this.operator();
+        boolean automaticUpdater = operator != Expressions.NULL_VALUE
+                && !this.isContains(insert.getColumns(), Expressions.UPDATER_COLUMN);
         if (automaticUpdater) {
             insert.addColumns(Expressions.UPDATER_COLUMN);
-            Expression operator = this.operator();
             ItemsList items = insert.getItemsList();
             if (items instanceof MultiExpressionList) {
                 ((MultiExpressionList) items).getExpressionLists().forEach(el -> el.getExpressions().add(operator));

+ 12 - 17
framework-database/src/main/java/com/chelvc/framework/database/support/AbstractCreateMethod.java

@@ -1,7 +1,5 @@
 package com.chelvc.framework.database.support;
 
-import java.util.List;
-
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.core.enums.SqlMethod;
 import com.baomidou.mybatisplus.core.injector.AbstractMethod;
@@ -33,40 +31,37 @@ public abstract class AbstractCreateMethod extends AbstractMethod {
     }
 
     /**
-     * 将字段转换成列
+     * 获取表新增字段信息
      *
-     * @param table  表信息
-     * @param fields 字段列表
-     * @return 列信息
+     * @param table 表信息
+     * @return 字段信息
      */
-    protected String field2column(TableInfo table, List<TableFieldInfo> fields) {
+    protected String getColumns(TableInfo table) {
         String columns = table.getKeyInsertSqlColumn(false) +
-                this.filterTableFieldInfo(fields, null, TableFieldInfo::getInsertSqlColumn, EMPTY);
+                this.filterTableFieldInfo(table.getFieldList(), null, TableFieldInfo::getInsertSqlColumn, EMPTY);
         return columns.substring(0, columns.length() - 1);
     }
 
     /**
-     * 将字段转换成属性
+     * 获取表新增属性
      *
-     * @param table  表信息
-     * @param fields 字段列表
+     * @param table 表信息
      * @return 属性信息
      */
-    protected String field2property(TableInfo table, List<TableFieldInfo> fields) {
-        return this.field2property(table, null, fields);
+    protected String getProperties(TableInfo table) {
+        return this.getProperties(table, null);
     }
 
     /**
-     * 将字段转换成属性
+     * 获取表新增属性
      *
      * @param table  表信息
      * @param prefix 属性前缀
-     * @param fields 字段列表
      * @return 属性信息
      */
-    protected String field2property(TableInfo table, String prefix, List<TableFieldInfo> fields) {
+    protected String getProperties(TableInfo table, String prefix) {
         String properties = table.getKeyInsertSqlProperty(prefix, false) +
-                this.filterTableFieldInfo(fields, null, i -> i.getInsertSqlProperty(prefix), EMPTY);
+                this.filterTableFieldInfo(table.getFieldList(), null, i -> i.getInsertSqlProperty(prefix), EMPTY);
         return properties.substring(0, properties.length() - 1);
     }
 

+ 2 - 6
framework-database/src/main/java/com/chelvc/framework/database/support/BatchCreateIgnoreMethod.java

@@ -1,8 +1,5 @@
 package com.chelvc.framework.database.support;
 
-import java.util.List;
-
-import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
 import lombok.NonNull;
 
@@ -19,9 +16,8 @@ public class BatchCreateIgnoreMethod extends AbstractCreateMethod {
 
     @Override
     protected String initializeMethodScript(@NonNull TableInfo table) {
-        List<TableFieldInfo> fields = table.getFieldList();
-        String columns = this.field2column(table, fields);
-        String properties = this.field2property(table, "entity.", fields);
+        String columns = this.getColumns(table);
+        String properties = this.getProperties(table, "entity.");
         return String.format(
                 "<script>\nINSERT IGNORE INTO %s (%s) VALUES\n" +
                         "<foreach collection=\"collection\" item=\"entity\" separator=\",\">(%s)</foreach>\n</script>",

+ 2 - 6
framework-database/src/main/java/com/chelvc/framework/database/support/BatchCreateUpdateMethod.java

@@ -1,8 +1,5 @@
 package com.chelvc.framework.database.support;
 
-import java.util.List;
-
-import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
 import lombok.NonNull;
 
@@ -19,9 +16,8 @@ public class BatchCreateUpdateMethod extends AbstractCreateMethod {
 
     @Override
     protected String initializeMethodScript(@NonNull TableInfo table) {
-        List<TableFieldInfo> fields = table.getFieldList();
-        String columns = this.field2column(table, fields);
-        String properties = this.field2property(table, "entity.", fields);
+        String columns = this.getColumns(table);
+        String properties = this.getProperties(table, "entity.");
         String updates = "<foreach collection=\"update.expressions.entrySet()\" " +
                 "index=\"column\" item=\"value\" separator=\",\">${column}${value}</foreach>";
         return String.format(

+ 2 - 6
framework-database/src/main/java/com/chelvc/framework/database/support/CreateIgnoreMethod.java

@@ -1,8 +1,5 @@
 package com.chelvc.framework.database.support;
 
-import java.util.List;
-
-import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
 import lombok.NonNull;
 
@@ -19,9 +16,8 @@ public class CreateIgnoreMethod extends AbstractCreateMethod {
 
     @Override
     protected String initializeMethodScript(@NonNull TableInfo table) {
-        List<TableFieldInfo> fields = table.getFieldList();
-        String columns = this.field2column(table, fields);
-        String properties = this.field2property(table, fields);
+        String columns = this.getColumns(table);
+        String properties = this.getProperties(table);
         return String.format(
                 "<script>\nINSERT IGNORE INTO %s (%s) VALUES (%s)\n</script>",
                 table.getTableName(), columns, properties

+ 2 - 6
framework-database/src/main/java/com/chelvc/framework/database/support/CreateUpdateMethod.java

@@ -1,8 +1,5 @@
 package com.chelvc.framework.database.support;
 
-import java.util.List;
-
-import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
 import lombok.NonNull;
 
@@ -19,9 +16,8 @@ public class CreateUpdateMethod extends AbstractCreateMethod {
 
     @Override
     protected String initializeMethodScript(@NonNull TableInfo table) {
-        List<TableFieldInfo> fields = table.getFieldList();
-        String columns = this.field2column(table, fields);
-        String properties = this.field2property(table, "entity.", fields);
+        String columns = this.getColumns(table);
+        String properties = this.getProperties(table, "entity.");
         String updates = "<foreach collection=\"update.expressions.entrySet()\" " +
                 "index=\"column\" item=\"value\" separator=\",\">${column}${value}</foreach>";
         return String.format(

+ 1 - 1
framework-database/src/main/java/com/chelvc/framework/database/support/EnhanceMapper.java

@@ -6,7 +6,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import org.apache.ibatis.annotations.Param;
 
 /**
- * 数据模型增强操作接口
+ * 数据模型增强操作接口,忽略字段验证策略配置
  *
  * @author Woody
  * @date 2024/2/27