Browse Source

支持Serial注解.

https://github.com/baomidou/mybatis-plus/issues/6698
nieqiurong 3 months ago
parent
commit
d21b78e848

+ 21 - 0
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/builder/Entity.java

@@ -114,6 +114,14 @@ public class Entity implements ITemplate {
     @Getter
     private boolean serialVersionUID = true;
 
+    /**
+     * 是否启用 {@link java.io.Serial} (需JAVA 14) 注解
+     *
+     * @since 3.5.11
+     */
+    @Getter
+    private boolean serialAnnotation;
+
     /**
      * 【实体】是否生成字段常量(默认 false)<br>
      * -----------------------------------<br>
@@ -412,6 +420,7 @@ public class Entity implements ITemplate {
         data.put("versionFieldName", this.versionColumnName);
         data.put("activeRecord", this.activeRecord);
         data.put("entitySerialVersionUID", this.serialVersionUID);
+        data.put("entitySerialAnnotation", this.serialAnnotation);
         data.put("entityColumnConstant", this.columnConstant);
         data.put("entityBuilderModel", this.chain);
         data.put("chainModel", this.chain);
@@ -500,6 +509,18 @@ public class Entity implements ITemplate {
             return this;
         }
 
+        /**
+         * 启用生成 {@link java.io.Serial} (需JAVA 14)
+         * <p>当开启了 {@link #serialVersionUID} 时,会增加 {@link java.io.Serial} 注解在此字段上</p>
+         *
+         * @return this
+         * @since 3.5.11
+         */
+        public Builder enableSerialAnnotation() {
+            this.entity.serialAnnotation = true;
+            return this;
+        }
+
         /**
          * 开启生成字段常量
          *

+ 3 - 0
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/TableInfo.java

@@ -263,6 +263,9 @@ public class TableInfo {
         }
         if (entity.isSerialVersionUID() || entity.isActiveRecord()) {
             this.importPackages.add(Serializable.class.getCanonicalName());
+            if (entity.isSerialAnnotation()) {
+                this.importPackages.add("java.io.Serial");
+            }
         }
         if (this.isConvert()) {
             this.importPackages.add(TableName.class.getCanonicalName());

+ 3 - 0
mybatis-plus-generator/src/main/resources/templates/entity.java.btl

@@ -26,6 +26,9 @@ public class ${entity} {
 <% } %>
 <% if(entitySerialVersionUID){ %>
 
+    <% if(entitySerialAnnotation) { %>
+    @Serial
+    <% } %>
     private static final long serialVersionUID = 1L;
 <% } %>
 <% var keyPropertyName; %>

+ 3 - 0
mybatis-plus-generator/src/main/resources/templates/entity.java.ej

@@ -26,6 +26,9 @@ public class #(entity) {
 #end
 #if(entitySerialVersionUID)
 
+    #if(entitySerialAnnotation)
+    @Serial
+    #end
     private static final long serialVersionUID = 1L;
 #end
 ### ----------  BEGIN 字段循环遍历  ----------

+ 3 - 0
mybatis-plus-generator/src/main/resources/templates/entity.java.ftl

@@ -25,6 +25,9 @@ public class ${entity} {
 </#if>
 <#if entitySerialVersionUID>
 
+    <#if entitySerialAnnotation>
+    @Serial
+    </#if>
     private static final long serialVersionUID = 1L;
 </#if>
 <#-- ----------  BEGIN 字段循环遍历  ---------->

+ 3 - 0
mybatis-plus-generator/src/main/resources/templates/entity.java.vm

@@ -26,6 +26,9 @@ public class ${entity} {
 #end
 #if(${entitySerialVersionUID})
 
+    #if(${entitySerialAnnotation})
+    @Serial
+    #end
     private static final long serialVersionUID = 1L;
 #end
 ## ----------  BEGIN 字段循环遍历  ----------