فهرست منبع

升级 2.0.9 更新吧! 骚年

= 8 سال پیش
والد
کامیت
c51e4f29cf

+ 12 - 2
src/main/java/com/baomidou/mybatisplus/generator/AutoGenerator.java

@@ -132,7 +132,9 @@ public class AutoGenerator {
     }
 
     /**
+     * <p>
      * 分析数据
+     * </p>
      *
      * @param config 总配置信息
      * @return 解析数据结果集
@@ -166,7 +168,7 @@ public class AutoGenerator {
                 // 表注解
                 tableInfo.setImportPackages("com.baomidou.mybatisplus.annotations.TableName");
             }
-            if (tableInfo.isLogicDelete(config.getGlobalConfig().getLogicDeletePropertyName())) {
+            if (tableInfo.isLogicDelete(config.getStrategyConfig().getLogicDeleteFieldName())) {
                 // 逻辑删除注解
                 tableInfo.setImportPackages("com.baomidou.mybatisplus.annotations.TableLogic");
             }
@@ -196,7 +198,7 @@ public class AutoGenerator {
             ctx.put("restControllerStyle", config.getStrategyConfig().isRestControllerStyle());
             ctx.put("package", packageInfo);
             ctx.put("author", config.getGlobalConfig().getAuthor());
-            ctx.put("logicDeletePropertyName", config.getGlobalConfig().getLogicDeletePropertyName());
+            ctx.put("logicDeleteFieldName", config.getStrategyConfig().getLogicDeleteFieldName());
             ctx.put("activeRecord", config.getGlobalConfig().isActiveRecord());
             ctx.put("date", date);
             ctx.put("table", tableInfo);
@@ -223,7 +225,9 @@ public class AutoGenerator {
     }
 
     /**
+     * <p>
      * 获取类名
+     * </p>
      *
      * @param classPath
      * @return
@@ -235,7 +239,9 @@ public class AutoGenerator {
     }
 
     /**
+     * <p>
      * 处理输出目录
+     * </p>
      *
      * @param pathInfo 路径信息
      */
@@ -252,7 +258,9 @@ public class AutoGenerator {
     }
 
     /**
+     * <p>
      * 合成上下文与模板
+     * </p>
      *
      * @param context vm上下文
      */
@@ -306,7 +314,9 @@ public class AutoGenerator {
     }
 
     /**
+     * <p>
      * 将模板转化成为文件
+     * </p>
      *
      * @param context      内容对象
      * @param templatePath 模板文件

+ 0 - 14
src/main/java/com/baomidou/mybatisplus/generator/config/GlobalConfig.java

@@ -73,11 +73,6 @@ public class GlobalConfig {
     private String serviceImplName;
     private String controllerName;
 
-    /**
-     * 逻辑删除属性名称
-     */
-    private String logicDeletePropertyName;
-
     public String getOutputDir() {
         return outputDir;
     }
@@ -194,13 +189,4 @@ public class GlobalConfig {
         this.controllerName = controllerName;
         return this;
     }
-
-    public String getLogicDeletePropertyName() {
-        return logicDeletePropertyName;
-    }
-
-    public void setLogicDeletePropertyName(String logicDeletePropertyName) {
-        this.logicDeletePropertyName = logicDeletePropertyName;
-    }
-
 }

+ 30 - 0
src/main/java/com/baomidou/mybatisplus/generator/config/StrategyConfig.java

@@ -15,6 +15,9 @@
  */
 package com.baomidou.mybatisplus.generator.config;
 
+import java.util.List;
+
+import com.baomidou.mybatisplus.generator.config.po.TableFill;
 import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
 import com.baomidou.mybatisplus.toolkit.StringUtils;
 
@@ -127,6 +130,15 @@ public class StrategyConfig {
      */
     private boolean controllerMappingHyphenStyle = false;
 
+    /**
+     * 逻辑删除属性名称
+     */
+    private String logicDeleteFieldName;
+
+    /**
+     * 表填充字段
+     */
+    private List<TableFill> tableFillList = null;
 
     public StrategyConfig setDbColumnUnderline(boolean dbColumnUnderline) {
         DB_COLUMN_UNDERLINE = dbColumnUnderline;
@@ -330,4 +342,22 @@ public class StrategyConfig {
         this.controllerMappingHyphenStyle = controllerMappingHyphenStyle;
         return this;
     }
+
+    public String getLogicDeleteFieldName() {
+        return logicDeleteFieldName;
+    }
+
+    public StrategyConfig setLogicDeleteFieldName(String logicDeletePropertyName) {
+        this.logicDeleteFieldName = logicDeleteFieldName;
+        return this;
+    }
+
+    public List<TableFill> getTableFillList() {
+        return tableFillList;
+    }
+
+    public StrategyConfig setTableFillList(List<TableFill> tableFillList) {
+        this.tableFillList = tableFillList;
+        return this;
+    }
 }

+ 46 - 2
src/main/java/com/baomidou/mybatisplus/generator/config/builder/ConfigBuilder.java

@@ -27,6 +27,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import com.baomidou.mybatisplus.enums.FieldIgnore;
 import com.baomidou.mybatisplus.generator.config.ConstVal;
 import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
 import com.baomidou.mybatisplus.generator.config.GlobalConfig;
@@ -34,6 +35,7 @@ import com.baomidou.mybatisplus.generator.config.PackageConfig;
 import com.baomidou.mybatisplus.generator.config.StrategyConfig;
 import com.baomidou.mybatisplus.generator.config.TemplateConfig;
 import com.baomidou.mybatisplus.generator.config.po.TableField;
+import com.baomidou.mybatisplus.generator.config.po.TableFill;
 import com.baomidou.mybatisplus.generator.config.po.TableInfo;
 import com.baomidou.mybatisplus.generator.config.rules.DbType;
 import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
@@ -97,7 +99,9 @@ public class ConfigBuilder {
     private GlobalConfig globalConfig;
 
     /**
+     * <p>
      * 在构造器中处理配置
+     * </p>
      *
      * @param packageConfig    包配置
      * @param dataSourceConfig 数据源配置
@@ -139,7 +143,9 @@ public class ConfigBuilder {
     // ************************ 曝露方法 BEGIN*****************************
 
     /**
+     * <p>
      * 所有包配置信息
+     * </p>
      *
      * @return 包配置
      */
@@ -148,7 +154,9 @@ public class ConfigBuilder {
     }
 
     /**
+     * <p>
      * 所有路径配置
+     * </p>
      *
      * @return 路径配置
      */
@@ -165,7 +173,9 @@ public class ConfigBuilder {
     }
 
     /**
+     * <p>
      * 获取超类定义
+     * </p>
      *
      * @return 完整超类名称
      */
@@ -182,7 +192,9 @@ public class ConfigBuilder {
     }
 
     /**
+     * <p>
      * 表信息
+     * </p>
      *
      * @return 所有表信息
      */
@@ -191,7 +203,9 @@ public class ConfigBuilder {
     }
 
     /**
+     * <p>
      * 模板路径配置信息
+     * </p>
      *
      * @return 所以模板路径配置信息
      */
@@ -202,7 +216,9 @@ public class ConfigBuilder {
     // ****************************** 曝露方法 END**********************************
 
     /**
+     * <p>
      * 处理包配置
+     * </p>
      *
      * @param template  TemplateConfig
      * @param outputDir
@@ -241,7 +257,9 @@ public class ConfigBuilder {
     }
 
     /**
+     * <p>
      * 处理数据源配置
+     * </p>
      *
      * @param config DataSourceConfig
      */
@@ -251,7 +269,9 @@ public class ConfigBuilder {
     }
 
     /**
+     * <p>
      * 处理数据库表 加载数据库表、列、注释相关数据集
+     * </p>
      *
      * @param config StrategyConfig
      */
@@ -261,7 +281,9 @@ public class ConfigBuilder {
     }
 
     /**
+     * <p>
      * 处理superClassName,IdClassType,IdStrategy配置
+     * </p>
      *
      * @param config 策略配置
      */
@@ -286,7 +308,9 @@ public class ConfigBuilder {
     }
 
     /**
+     * <P>
      * 处理表对应的类名称
+     * </P>
      *
      * @param tableList   表名称
      * @param strategy    命名策略
@@ -326,9 +350,9 @@ public class ConfigBuilder {
     }
 
     /**
+     * <p>
      * 获取所有的数据库表信息
-     *
-     * @return 表信息
+     * </p>
      */
     private List<TableInfo> getTablesInfo(StrategyConfig config) {
         boolean isInclude = (null != config.getInclude() && config.getInclude().length > 0);
@@ -418,7 +442,9 @@ public class ConfigBuilder {
 
 
     /**
+     * <p>
      * 判断主键是否为identity,目前仅对mysql进行检查
+     * </p>
      *
      * @param results ResultSet
      * @return 主键是否为identity
@@ -479,6 +505,16 @@ public class ConfigBuilder {
                     commonFieldList.add(field);
                     continue;
                 }
+                // 填充逻辑判断
+                List<TableFill> tfs = this.getStrategyConfig().getTableFillList();
+                if (null != tfs) {
+                    for (TableFill tf : tfs) {
+                        if (tf.getFieldName().equals(field.getName())) {
+                            field.setIgnore(FieldIgnore.INSERT.name());
+                            break;
+                        }
+                    }
+                }
                 fieldList.add(field);
             }
         } catch (SQLException e) {
@@ -490,7 +526,9 @@ public class ConfigBuilder {
     }
 
     /**
+     * <p>
      * 连接路径字符串
+     * </p>
      *
      * @param parentDir   路径常量字符串
      * @param packageName 包名
@@ -508,7 +546,9 @@ public class ConfigBuilder {
     }
 
     /**
+     * <p>
      * 连接父子包名
+     * </p>
      *
      * @param parent     父包名
      * @param subPackage 子包名
@@ -522,7 +562,9 @@ public class ConfigBuilder {
     }
 
     /**
+     * <p>
      * 处理字段名称
+     * </p>
      *
      * @return 根据策略返回处理后的名称
      */
@@ -531,7 +573,9 @@ public class ConfigBuilder {
     }
 
     /**
+     * <p>
      * 处理字段名称
+     * </p>
      *
      * @param name
      * @param strategy

+ 9 - 0
src/main/java/com/baomidou/mybatisplus/generator/config/po/TableField.java

@@ -15,6 +15,7 @@
  */
 package com.baomidou.mybatisplus.generator.config.po;
 
+import com.baomidou.mybatisplus.enums.FieldIgnore;
 import com.baomidou.mybatisplus.generator.config.StrategyConfig;
 import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
 import com.baomidou.mybatisplus.toolkit.StringUtils;
@@ -40,6 +41,7 @@ public class TableField {
     private String propertyName;
     private DbColumnType columnType;
     private String comment;
+    private String ignore;
 
     public boolean isConvert() {
         return convert;
@@ -145,4 +147,11 @@ public class TableField {
         return firstChar.toUpperCase() + propertyName.substring(1);
     }
 
+    public String getIgnore() {
+        return ignore;
+    }
+
+    public void setIgnore(String ignore) {
+        this.ignore = ignore;
+    }
 }

+ 59 - 0
src/main/java/com/baomidou/mybatisplus/generator/config/po/TableFill.java

@@ -0,0 +1,59 @@
+/**
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.generator.config.po;
+
+import com.baomidou.mybatisplus.enums.FieldIgnore;
+
+/**
+ * <p>
+ * 字段填充
+ * </p>
+ *
+ * @author hubin
+ * @since 2017-06-26
+ */
+public class TableFill {
+
+    /* 字段名称 */
+    private String fieldName;
+    /* 忽略类型 */
+    private FieldIgnore ignore;
+
+    private TableFill() {
+        // to do nothing
+    }
+
+    public TableFill(String fieldName, FieldIgnore ignore) {
+        this.fieldName = fieldName;
+        this.ignore = ignore;
+    }
+
+    public String getFieldName() {
+        return fieldName;
+    }
+
+    public void setFieldName(String fieldName) {
+        this.fieldName = fieldName;
+    }
+
+    public FieldIgnore getIgnore() {
+        return ignore;
+    }
+
+    public void setIgnore(FieldIgnore ignore) {
+        this.ignore = ignore;
+    }
+}

+ 7 - 1
src/main/resources/templates/entity.java.vm

@@ -48,10 +48,16 @@ public class ${entity} implements Serializable {
 #elseif(${field.convert})
     @TableId("${field.name}")
 #end
+#elseif(${field.ignore})
+#if(${field.convert})
+	@TableField(value = "${field.name}", ignore = FieldIgnore.${field.ignore})
+#else
+	@TableField(ignore = FieldIgnore.${field.ignore})
+#end
 #elseif(${field.convert})
 	@TableField("${field.name}")
 #end
-#if(${logicDeletePropertyName}==${field.propertyName})
+#if(${logicDeleteFieldName}==${field.name})
     @TableLogic
 #end
 	private ${field.propertyType} ${field.propertyName};

+ 7 - 3
src/test/java/com/baomidou/mybatisplus/test/IdWorkerTest.java

@@ -41,7 +41,13 @@ import com.baomidou.mybatisplus.toolkit.IdWorker;
  */
 public class IdWorkerTest {
 
-    @Test
+    public static void main(String[] args) throws Exception {
+        IdWorkerTest iwt = new IdWorkerTest();
+        iwt.test();
+        iwt.test1();
+        iwt.test2();
+    }
+
     public void test() throws Exception {
         int count = 1000;
         System.err.println("共有" + count + "个数参与测试");
@@ -83,7 +89,6 @@ public class IdWorkerTest {
         System.err.println("偶数:" + even);
     }
 
-    @Test
     public void test1() throws Exception {
         // 毫秒内并发
         for (int i = 0; i < 1000; i++) {
@@ -92,7 +97,6 @@ public class IdWorkerTest {
     }
 
 
-    @Test
     public void test2() throws Exception {
         // 随机尾数
         for (int i = 0; i < 1000; i++) {

+ 8 - 1
src/test/java/com/baomidou/mybatisplus/test/generator/MysqlGenerator.java

@@ -22,6 +22,7 @@ import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
 
+import com.baomidou.mybatisplus.enums.FieldIgnore;
 import com.baomidou.mybatisplus.generator.AutoGenerator;
 import com.baomidou.mybatisplus.generator.InjectionConfig;
 import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
@@ -31,6 +32,7 @@ import com.baomidou.mybatisplus.generator.config.PackageConfig;
 import com.baomidou.mybatisplus.generator.config.StrategyConfig;
 import com.baomidou.mybatisplus.generator.config.TemplateConfig;
 import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
+import com.baomidou.mybatisplus.generator.config.po.TableFill;
 import com.baomidou.mybatisplus.generator.config.po.TableInfo;
 import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
 import com.baomidou.mybatisplus.generator.config.rules.DbType;
@@ -52,6 +54,10 @@ public class MysqlGenerator {
      * </p>
      */
     public static void main(String[] args) {
+        // 自定义需要填充的字段
+        List<TableFill> tableFillList = new ArrayList<>();
+        tableFillList.add(new TableFill("ASDD_SS", FieldIgnore.INSERT_UPDATE));
+
         // 代码生成器
         AutoGenerator mpg = new AutoGenerator().setGlobalConfig(
                 // 全局配置
@@ -100,7 +106,8 @@ public class MysqlGenerator {
                         // 自定义实体父类
                         // .setSuperEntityClass("com.baomidou.demo.TestEntity")
                         // 自定义实体,公共字段
-                        .setSuperEntityColumns(new String[]{"test_id", "age"})
+                        .setSuperEntityColumns(new String[]{"test_id"})
+                        .setTableFillList(tableFillList)
                 // 自定义 mapper 父类
                 // .setSuperMapperClass("com.baomidou.demo.TestMapper")
                 // 自定义 service 父类