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

支持lombok模式下选择是否进行链式set生成.

https://github.com/baomidou/mybatis-plus/issues/2357
聂秋秋 пре 5 година
родитељ
комит
0502937185

+ 40 - 0
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/StrategyConfig.java

@@ -123,8 +123,21 @@ public class StrategyConfig {
      * 【实体】是否为构建者模型(默认 false)<br>
      * -----------------------------------<br>
      * public User setName(String name) { this.name = name; return this; }
+     *
+     * @deprecated 3.3.2 {@link #setChainModel(boolean)}
      */
+    @Deprecated
     private boolean entityBuilderModel = false;
+    
+    /**
+     * 【实体】是否为链式模型(默认 false)<br>
+     * -----------------------------------<br>
+     * public User setName(String name) { this.name = name; return this; }
+     *
+     * @since 3.3.2
+     */
+    private boolean chainModel = false;
+    
     /**
      * 【实体】是否为lombok模型(默认 false)<br>
      * <a href="https://projectlombok.org/">document</a>
@@ -369,4 +382,31 @@ public class StrategyConfig {
         entityTableFieldAnnotationEnable = isEnableAnnotation;
         return this;
     }
+    
+    /**
+     * 是否为构建者模型
+     *
+     * @param entityBuilderModel entityBuilderModel
+     * @return this
+     * @deprecated 3.3.2 {@link #setChainModel(boolean)}
+     */
+    @Deprecated
+    public StrategyConfig setEntityBuilderModel(boolean entityBuilderModel) {
+        this.entityBuilderModel = entityBuilderModel;
+        this.chainModel = entityBuilderModel;
+        return this;
+    }
+    
+    /**
+     * 是否为链式模型
+     *
+     * @param chainModel 链式模型
+     * @return this
+     */
+    public StrategyConfig setChainModel(boolean chainModel) {
+        this.chainModel = chainModel;
+        this.entityBuilderModel = chainModel;
+        return this;
+    }
+    
 }

+ 1 - 0
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/engine/AbstractTemplateEngine.java

@@ -218,6 +218,7 @@ public abstract class AbstractTemplateEngine {
         objectMap.put("entitySerialVersionUID", config.getStrategyConfig().isEntitySerialVersionUID());
         objectMap.put("entityColumnConstant", config.getStrategyConfig().isEntityColumnConstant());
         objectMap.put("entityBuilderModel", config.getStrategyConfig().isEntityBuilderModel());
+        objectMap.put("chainModel", config.getStrategyConfig().isChainModel());
         objectMap.put("entityLombokModel", config.getStrategyConfig().isEntityLombokModel());
         objectMap.put("entityBooleanColumnRemoveIsPrefix", config.getStrategyConfig().isEntityBooleanColumnRemoveIsPrefix());
         objectMap.put("superEntityClass", getSuperClassName(config.getSuperEntityClass()));

+ 6 - 2
mybatis-plus-generator/src/main/resources/templates/entity.java.btl

@@ -9,8 +9,10 @@ import io.swagger.annotations.ApiModelProperty;
 <% if(entityLombokModel){ %>
 import lombok.Data;
 import lombok.EqualsAndHashCode;
+<% if(chainModel){ %>
 import lombok.experimental.Accessors;
 <% } %>
+<% } %>
 /**
  * <p>
  * ${table.comment!}
@@ -26,7 +28,9 @@ import lombok.experimental.Accessors;
     <% }else{ %>
 @EqualsAndHashCode(callSuper = false)
     <% } %>
+    <% if(chainModel){ %>
 @Accessors(chain = true)
+    <% } %>
 <% } %>
 <% if(table.convert){ %>
 @TableName("${table.name}")
@@ -115,13 +119,13 @@ public class ${entity} implements Serializable {
         return ${field.propertyName};
     }
 
-        <% if(entityBuilderModel){ %>
+        <% if(chainModel){ %>
     public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
         <% }else{ %>
     public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
         <% } %>
         this.${field.propertyName} = ${field.propertyName};
-        <% if(entityBuilderModel){ %>
+        <% if(chainModel){ %>
         return this;
         <% } %>
     }

+ 6 - 2
mybatis-plus-generator/src/main/resources/templates/entity.java.ftl

@@ -10,7 +10,9 @@ import io.swagger.annotations.ApiModelProperty;
 <#if entityLombokModel>
 import lombok.Data;
 import lombok.EqualsAndHashCode;
+    <#if chainModel>
 import lombok.experimental.Accessors;
+    </#if>
 </#if>
 
 /**
@@ -28,7 +30,9 @@ import lombok.experimental.Accessors;
     <#else>
 @EqualsAndHashCode(callSuper = false)
     </#if>
+    <#if chainModel>
 @Accessors(chain = true)
+    </#if>
 </#if>
 <#if table.convert>
 @TableName("${table.name}")
@@ -105,13 +109,13 @@ public class ${entity} implements Serializable {
         return ${field.propertyName};
     }
 
-    <#if entityBuilderModel>
+    <#if chainModel>
     public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
     <#else>
     public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
     </#if>
         this.${field.propertyName} = ${field.propertyName};
-        <#if entityBuilderModel>
+        <#if chainModel>
         return this;
         </#if>
     }

+ 6 - 2
mybatis-plus-generator/src/main/resources/templates/entity.java.vm

@@ -10,8 +10,10 @@ import io.swagger.annotations.ApiModelProperty;
 #if(${entityLombokModel})
 import lombok.Data;
 import lombok.EqualsAndHashCode;
+#if(${chainModel})
 import lombok.experimental.Accessors;
 #end
+#end
 
 /**
  * <p>
@@ -28,7 +30,9 @@ import lombok.experimental.Accessors;
   #else
 @EqualsAndHashCode(callSuper = false)
   #end
+  #if(${chainModel})
 @Accessors(chain = true)
+  #end
 #end
 #if(${table.convert})
 @TableName("${table.name}")
@@ -106,13 +110,13 @@ public class ${entity} implements Serializable {
         return ${field.propertyName};
     }
 
-  #if(${entityBuilderModel})
+  #if(${chainModel})
     public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
   #else
     public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
   #end
         this.${field.propertyName} = ${field.propertyName};
-  #if(${entityBuilderModel})
+  #if(${chainModel})
         return this;
   #end
     }