Browse Source

支持 update set 字段自定义注入 fixed gitee IHART

= 7 years ago
parent
commit
aed9c4bb52

+ 17 - 2
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/annotations/TableField.java

@@ -49,7 +49,7 @@ public @interface TableField {
      * </p>
      * <p>
      * 支持:@TableField(el = "role, jdbcType=BIGINT)<br>
-     * 支持:@TableField(el = "role, typeHandler=com.baomidou.xx.typehandler.PhoneTypeHandler")
+     * 支持:@TableField(el = "role, typeHandler=com.baomidou.springcloud.typehandler.PhoneTypeHandler")
      * </p>
      */
     String el() default "";
@@ -66,7 +66,7 @@ public @interface TableField {
 
     /**
      * <p>
-     * 字段 WHERE 实体查询比较条件
+     * 字段 where 实体查询比较条件
      * </p>
      * <p>
      * 默认 `=` 等值
@@ -74,6 +74,21 @@ public @interface TableField {
      */
     String condition() default SqlCondition.EQUAL;
 
+    /**
+     * <p>
+     * 字段 update set 部分注入, 该注解优于 el 注解使用
+     * </p>
+     * <p>
+     * 例如:@TableField(.. , update="%s+1") 其中 %s 会填充为字段
+     * 输出 SQL 为:update 表 set 字段=字段+1 where ...
+     * </p>
+     * <p>
+     * 例如:@TableField(.. , update="now()") 使用数据库时间
+     * 输出 SQL 为:update 表 set 字段=now() where ...
+     * </p>
+     */
+    String update() default "";
+
     /**
      * <p>
      * 字段验证策略

+ 15 - 1
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/entity/TableFieldInfo.java

@@ -78,7 +78,12 @@ public class TableFieldInfo {
     private String logicNotDeleteValue;
 
     /**
-     * 字段比较条件
+     * 字段 update set 部分注入
+     */
+    private String update;
+
+    /**
+     * where 字段比较条件
      */
     private String condition = SqlCondition.EQUAL;
 
@@ -123,6 +128,7 @@ public class TableFieldInfo {
             this.fieldStrategy = globalConfig.getFieldStrategy();
         }
         tableInfo.setLogicDelete(this.initLogicDelete(globalConfig, field));
+        this.update = tableField.update();
         this.condition = tableField.condition();
         /*
          * 保存当前字段的填充策略
@@ -252,6 +258,14 @@ public class TableFieldInfo {
         this.logicNotDeleteValue = logicNotDeleteValue;
     }
 
+    public String getUpdate() {
+        return update;
+    }
+
+    public void setUpdate(String update) {
+        this.update = update;
+    }
+
     public String getCondition() {
         return condition;
     }

+ 33 - 26
mybatis-plus-support/src/main/java/com/baomidou/mybatisplus/mapper/AutoSqlInjector.java

@@ -72,7 +72,9 @@ public class AutoSqlInjector implements ISqlInjector {
     protected MapperBuilderAssistant builderAssistant;
 
     /**
-     * CRUD注入后给予标识 注入过后不再注入
+     * <p>
+     * CRUD 注入后给予标识 注入过后不再注入
+     * </p>
      *
      * @param builderAssistant
      * @param mapperClass
@@ -124,13 +126,13 @@ public class AutoSqlInjector implements ISqlInjector {
          * #148 表信息包含主键,注入主键相关方法
          */
         if (StringUtils.isNotEmpty(table.getKeyProperty())) {
-            /* 删除 */
+            /** 删除 */
             this.injectDeleteByIdSql(false, mapperClass, modelClass, table);
             this.injectDeleteByIdSql(true, mapperClass, modelClass, table);
-            /* 修改 */
+            /** 修改 */
             this.injectUpdateByIdSql(true, mapperClass, modelClass, table);
             this.injectUpdateByIdSql(false, mapperClass, modelClass, table);
-            /* 查询 */
+            /** 查询 */
             this.injectSelectByIdSql(false, mapperClass, modelClass, table);
             this.injectSelectByIdSql(true, mapperClass, modelClass, table);
         } else {
@@ -141,15 +143,15 @@ public class AutoSqlInjector implements ISqlInjector {
         /**
          * 正常注入无需主键方法
          */
-        /* 插入 */
+        /** 插入 */
         this.injectInsertOneSql(true, mapperClass, modelClass, table);
         this.injectInsertOneSql(false, mapperClass, modelClass, table);
-        /* 删除 */
+        /** 删除 */
         this.injectDeleteSql(mapperClass, modelClass, table);
         this.injectDeleteByMapSql(mapperClass, table);
-        /* 修改 */
+        /** 修改 */
         this.injectUpdateSql(mapperClass, modelClass, table);
-        /* 查询 */
+        /** 查询 */
         this.injectSelectByMapSql(mapperClass, modelClass, table);
         this.injectSelectOneSql(mapperClass, modelClass, table);
         this.injectSelectCountSql(mapperClass, modelClass, table);
@@ -158,7 +160,7 @@ public class AutoSqlInjector implements ISqlInjector {
         this.injectSelectMapsSql(SqlMethod.SELECT_MAPS, mapperClass, modelClass, table);
         this.injectSelectMapsSql(SqlMethod.SELECT_MAPS_PAGE, mapperClass, modelClass, table);
         this.injectSelectObjsSql(SqlMethod.SELECT_OBJS, mapperClass, modelClass, table);
-        /* 自定义方法 */
+        /** 自定义方法 */
         this.inject(configuration, builderAssistant, mapperClass, modelClass, table);
     }
 
@@ -229,7 +231,7 @@ public class AutoSqlInjector implements ISqlInjector {
         // 表包含主键处理逻辑,如果不包含主键当普通字段处理
         if (StringUtils.isNotEmpty(table.getKeyProperty())) {
             if (table.getIdType() == IdType.AUTO) {
-                /* 自增主键 */
+                /** 自增主键 */
                 keyGenerator = new Jdbc3KeyGenerator();
                 keyProperty = table.getKeyProperty();
                 keyColumn = table.getKeyColumn();
@@ -241,7 +243,7 @@ public class AutoSqlInjector implements ISqlInjector {
                     fieldBuilder.append(table.getKeyColumn()).append(",");
                     placeholderBuilder.append("#{").append(table.getKeyProperty()).append("},");
                 } else {
-                    /* 用户输入自定义ID */
+                    /** 用户输入自定义ID */
                     fieldBuilder.append(table.getKeyColumn()).append(",");
                     // 正常自定义主键策略
                     placeholderBuilder.append("#{").append(table.getKeyProperty()).append("},");
@@ -565,13 +567,18 @@ public class AutoSqlInjector implements ISqlInjector {
             ifTag = !(FieldFill.UPDATE == fieldInfo.getFieldFill()
                     || FieldFill.INSERT_UPDATE == fieldInfo.getFieldFill());
             if (selective && ifTag) {
-                set.append(convertIfTag(true, fieldInfo, prefix, false));
-                set.append(fieldInfo.getColumn()).append("=#{");
-                if (null != prefix) {
-                    set.append(prefix);
+                if (StringUtils.isNotEmpty(fieldInfo.getUpdate())) {
+                    set.append(fieldInfo.getColumn()).append("=");
+                    set.append(String.format(fieldInfo.getUpdate(), fieldInfo.getColumn())).append(",");
+                } else {
+                    set.append(convertIfTag(true, fieldInfo, prefix, false));
+                    set.append(fieldInfo.getColumn()).append("=#{");
+                    if (null != prefix) {
+                        set.append(prefix);
+                    }
+                    set.append(fieldInfo.getEl()).append("},");
+                    set.append(convertIfTag(true, fieldInfo, null, true));
                 }
-                set.append(fieldInfo.getEl()).append("},");
-                set.append(convertIfTag(true, fieldInfo, null, true));
             } else if (FieldFill.INSERT != fieldInfo.getFieldFill()) {
                 // 排除填充注解字段
                 set.append(fieldInfo.getColumn()).append("=#{");
@@ -774,7 +781,7 @@ public class AutoSqlInjector implements ISqlInjector {
      * @return
      */
     protected String convertIfTag(boolean ignored, TableFieldInfo fieldInfo, String prefix, boolean close) {
-        /* 忽略策略 */
+        /** 忽略策略 */
         FieldStrategy fieldStrategy = fieldInfo.getFieldStrategy();
         if (fieldStrategy == FieldStrategy.IGNORED) {
             if (ignored) {
@@ -789,7 +796,7 @@ public class AutoSqlInjector implements ISqlInjector {
             return "</if>";
         }
 
-		/* 前缀处理 */
+		/** 前缀处理 */
         String property = fieldInfo.getProperty();
         Class propertyType = fieldInfo.getPropertyType();
         property = StringUtils.removeIsPrefixIfBoolean(property, propertyType);
@@ -821,7 +828,7 @@ public class AutoSqlInjector implements ISqlInjector {
         return convertIfTag(fieldInfo, null, close);
     }
 
-    /*
+    /**
      * 查询
      */
     public MappedStatement addSelectMappedStatement(Class<?> mapperClass, String id, SqlSource sqlSource, Class<?> resultType,
@@ -829,18 +836,18 @@ public class AutoSqlInjector implements ISqlInjector {
         if (null != table) {
             String resultMap = table.getResultMap();
             if (null != resultMap) {
-                /* 返回 resultMap 映射结果集 */
+                /** 返回 resultMap 映射结果集 */
                 return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.SELECT, null, resultMap, null,
                         new NoKeyGenerator(), null, null);
             }
         }
 
-		/* 普通查询 */
+		/** 普通查询 */
         return this.addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.SELECT, null, null, resultType,
                 new NoKeyGenerator(), null, null);
     }
 
-    /*
+    /**
      * 插入
      */
     public MappedStatement addInsertMappedStatement(Class<?> mapperClass, Class<?> modelClass, String id, SqlSource sqlSource,
@@ -849,7 +856,7 @@ public class AutoSqlInjector implements ISqlInjector {
                 keyGenerator, keyProperty, keyColumn);
     }
 
-    /*
+    /**
      * 删除
      */
     public MappedStatement addDeleteMappedStatement(Class<?> mapperClass, String id, SqlSource sqlSource) {
@@ -857,7 +864,7 @@ public class AutoSqlInjector implements ISqlInjector {
                 new NoKeyGenerator(), null, null);
     }
 
-    /*
+    /**
      * 更新
      */
     public MappedStatement addUpdateMappedStatement(Class<?> mapperClass, Class<?> modelClass, String id, SqlSource sqlSource) {
@@ -874,7 +881,7 @@ public class AutoSqlInjector implements ISqlInjector {
                     + "} Has been loaded by XML or SqlProvider, ignoring the injection of the SQL.");
             return null;
         }
-		/* 缓存逻辑处理 */
+		/** 缓存逻辑处理 */
         boolean isSelect = false;
         if (sqlCommandType == SqlCommandType.SELECT) {
             isSelect = true;