Browse Source

支持表注解自定义处理.

nieqiurong 3 months ago
parent
commit
c8aa180291

+ 82 - 0
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/DefaultTableAnnotationHandler.java

@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2011-2024, baomidou (jobob@qq.com).
+ *
+ * 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
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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;
+
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import com.baomidou.mybatisplus.generator.config.GlobalConfig;
+import com.baomidou.mybatisplus.generator.config.builder.Entity;
+import com.baomidou.mybatisplus.generator.config.po.TableInfo;
+import com.baomidou.mybatisplus.generator.model.ClassAnnotationAttributes;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 表注解处理器
+ *
+ * @author nieqiurong
+ * @since 3.5.10
+ */
+public class DefaultTableAnnotationHandler implements ITableAnnotationHandler {
+
+    @Override
+    public List<ClassAnnotationAttributes> handle(TableInfo tableInfo, Entity entity) {
+        List<ClassAnnotationAttributes> annotationAttributesList = new ArrayList<>();
+        GlobalConfig globalConfig = tableInfo.getGlobalConfig();
+        String comment = tableInfo.getComment();
+        if (StringUtils.isBlank(comment)) {
+            comment = StringPool.EMPTY;
+        }
+        boolean kotlin = globalConfig.isKotlin();
+        if (!kotlin) {
+            // 原先kt模板没有处理这些,作为兼容项
+            if (entity.isChain()) {
+                annotationAttributesList.add(new ClassAnnotationAttributes("@Accessors(chain = true)", "lombok.experimental.Accessors"));
+            }
+            if (entity.isLombok() && entity.isDefaultLombok()) {
+                // 原先lombok默认只有这两个
+                annotationAttributesList.add(new ClassAnnotationAttributes("@Getter", "lombok.Getter"));
+                annotationAttributesList.add(new ClassAnnotationAttributes("@Setter", "lombok.Setter"));
+            }
+        }
+        if (tableInfo.isConvert()) {
+            String schemaName = tableInfo.getSchemaName();
+            if (StringUtils.isBlank(schemaName)) {
+                schemaName = StringPool.EMPTY;
+            } else {
+                schemaName = schemaName + StringPool.DOT;
+            }
+            //@TableName("${schemaName}${table.name}")
+            String displayName = String.format("@TableName(\"%s%s\")", schemaName, tableInfo.getName());
+            annotationAttributesList.add(new ClassAnnotationAttributes(TableName.class, displayName));
+        }
+        if (globalConfig.isSwagger()) {
+            //@ApiModel(value = "${entity}对象", description = "${table.comment!}")
+            String displayName = String.format("@ApiModel(value = \"%s对象\", description = \"%s\")", tableInfo.getEntityName(), comment);
+            annotationAttributesList.add(new ClassAnnotationAttributes(
+                displayName, "io.swagger.annotations.ApiModel", "io.swagger.annotations.ApiModelProperty"));
+        }
+        if (globalConfig.isSpringdoc()) {
+            //@Schema(name = "${entity}", description = "${table.comment!}")
+            String displayName = String.format("@Schema(name = \"%s\", description = \"%s\")", tableInfo.getEntityName(), comment);
+            annotationAttributesList.add(new ClassAnnotationAttributes(displayName, "io.swagger.v3.oas.annotations.media.Schema"));
+        }
+        return annotationAttributesList;
+    }
+
+}

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

@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2011-2024, baomidou (jobob@qq.com).
+ *
+ * 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
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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;
+
+import com.baomidou.mybatisplus.generator.config.builder.Entity;
+import com.baomidou.mybatisplus.generator.config.po.TableInfo;
+import com.baomidou.mybatisplus.generator.model.ClassAnnotationAttributes;
+
+import java.util.List;
+
+/**
+ * 表注解处理器
+ *
+ * @author nieqiurong
+ * @since 3.5.10
+ */
+public interface ITableAnnotationHandler {
+
+    /**
+     * 处理字段级注解
+     *
+     * @param tableInfo 表信息
+     * @return 注解信息
+     */
+    List<ClassAnnotationAttributes> handle(TableInfo tableInfo, Entity entity);
+
+}

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

@@ -18,13 +18,13 @@ package com.baomidou.mybatisplus.generator.config.builder;
 import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
-import com.baomidou.mybatisplus.annotation.TableName;
 import com.baomidou.mybatisplus.core.handlers.AnnotationHandler;
 import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
-import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import com.baomidou.mybatisplus.generator.DefaultTableAnnotationHandler;
 import com.baomidou.mybatisplus.generator.DefaultTableFieldAnnotationHandler;
 import com.baomidou.mybatisplus.generator.IFill;
+import com.baomidou.mybatisplus.generator.ITableAnnotationHandler;
 import com.baomidou.mybatisplus.generator.ITableFieldAnnotationHandler;
 import com.baomidou.mybatisplus.generator.ITemplate;
 import com.baomidou.mybatisplus.generator.config.ConstVal;
@@ -235,6 +235,7 @@ public class Entity implements ITemplate {
     /**
      * 默认lombok (兼容属性只有Getter和Setter)
      */
+    @Getter
     private boolean defaultLombok = true;
 
     /**
@@ -245,6 +246,13 @@ public class Entity implements ITemplate {
     @Getter
     private final List<ClassAnnotationAttributes> classAnnotations = new ArrayList<>();
 
+    /**
+     * 表注解处理器
+     *
+     * @since 3.5.10
+     */
+    private ITableAnnotationHandler tableAnnotationHandler = new DefaultTableAnnotationHandler();
+
     /**
      * 字段注解处理器
      *
@@ -371,45 +379,18 @@ public class Entity implements ITemplate {
         data.put("entityLombokModel", this.lombok);
         data.put("entityBooleanColumnRemoveIsPrefix", this.booleanColumnRemoveIsPrefix);
         data.put("superEntityClass", ClassUtils.getSimpleName(this.superClass));
-        GlobalConfig globalConfig = tableInfo.getGlobalConfig();
-        String comment = tableInfo.getComment();
         Set<String> importPackages = new HashSet<>(tableInfo.getImportPackages());
-        if (StringUtils.isBlank(comment)) {
-            comment = StringPool.EMPTY;
-        }
-        boolean kotlin = globalConfig.isKotlin();
-        if (!kotlin) {
-            // 原先kt模板没有处理这些,作为兼容项
-            if (chain) {
-                this.classAnnotations.add(new ClassAnnotationAttributes("@Accessors(chain = true)", "lombok.experimental.Accessors"));
-            }
-            if (lombok && defaultLombok) {
-                // 原先lombok默认只有这两个
-                this.classAnnotations.add(new ClassAnnotationAttributes("@Getter", "lombok.Getter"));
-                this.classAnnotations.add(new ClassAnnotationAttributes("@Setter", "lombok.Setter"));
-            }
-        }
-        if (tableInfo.isConvert()) {
-            String schemaName = tableInfo.getSchemaName();
-            if (StringUtils.isBlank(schemaName)) {
-                schemaName = StringPool.EMPTY;
-            } else {
-                schemaName = schemaName + StringPool.DOT;
+        GlobalConfig globalConfig = tableInfo.getGlobalConfig();
+        List<ClassAnnotationAttributes> classAnnotationAttributes = new ArrayList<>();
+        if (tableAnnotationHandler != null) {
+            List<ClassAnnotationAttributes> classAnnotationAttributesList = tableAnnotationHandler.handle(tableInfo, this);
+            if (classAnnotationAttributesList != null && !classAnnotationAttributesList.isEmpty()) {
+                classAnnotationAttributes = classAnnotationAttributesList;
+                classAnnotationAttributesList.forEach(attributes -> {
+                    attributes.handleDisplayName(tableInfo);
+                    importPackages.addAll(attributes.getImportPackages());
+                });
             }
-            //@TableName("${schemaName}${table.name}")
-            String displayName = String.format("@TableName(\"%s%s\")", schemaName, tableInfo.getName());
-            this.classAnnotations.add(new ClassAnnotationAttributes(TableName.class, displayName));
-        }
-        if (globalConfig.isSwagger()) {
-            //@ApiModel(value = "${entity}对象", description = "${table.comment!}")
-            String displayName = String.format("@ApiModel(value = \"%s对象\", description = \"%s\")", tableInfo.getEntityName(), comment);
-            this.classAnnotations.add(new ClassAnnotationAttributes(
-                displayName, "io.swagger.annotations.ApiModel", "io.swagger.annotations.ApiModelProperty"));
-        }
-        if (globalConfig.isSpringdoc()) {
-            //@Schema(name = "${entity}", description = "${table.comment!}")
-            String displayName = String.format("@Schema(name = \"%s\", description = \"%s\")", tableInfo.getEntityName(), comment);
-            this.classAnnotations.add(new ClassAnnotationAttributes(displayName, "io.swagger.v3.oas.annotations.media.Schema"));
         }
         if (tableFieldAnnotationHandler != null) {
             tableInfo.getFields().forEach(tableField -> {
@@ -420,13 +401,9 @@ public class Entity implements ITemplate {
                 }
             });
         }
-        this.classAnnotations.forEach(attributes -> {
-            attributes.handleDisplayName(tableInfo);
-            importPackages.addAll(attributes.getImportPackages());
-        });
         //TODO 外部暂时不要使用此属性,内置模板暂时使用
         data.put("useJavaDoc", !(globalConfig.isSwagger() || globalConfig.isSpringdoc()));
-        data.put("entityClassAnnotations", this.classAnnotations.stream()
+        data.put("entityClassAnnotations", classAnnotationAttributes.stream()
             .sorted(Comparator.comparingInt(s -> s.getDisplayName().length())).collect(Collectors.toList()));
         data.put("importEntityPackages", importPackages.stream().sorted().collect(Collectors.toList()));
         return data;
@@ -803,6 +780,17 @@ public class Entity implements ITemplate {
             return this;
         }
 
+        /**
+         * 指定表注解处理器
+         * @param tableAnnotationHandler 表注解处理器
+         * @since 3.5.10
+         * @return this
+         */
+        public Builder tableAnnotationHandler(@NotNull ITableAnnotationHandler tableAnnotationHandler){
+            this.entity.tableAnnotationHandler = tableAnnotationHandler;
+            return this;
+        }
+
         public Entity get() {
             String superClass = this.entity.superClass;
             if (StringUtils.isNotBlank(superClass)) {