瀏覽代碼

代码生成器,支持自定义判断文件是否覆盖创建接口

hubin 7 年之前
父節點
當前提交
4f67b343a2

+ 6 - 0
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/InjectionConfig.java

@@ -19,6 +19,7 @@ import java.util.List;
 import java.util.Map;
 
 import com.baomidou.mybatisplus.generator.config.FileOutConfig;
+import com.baomidou.mybatisplus.generator.config.IFileCreate;
 import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
 
 import lombok.Data;
@@ -51,6 +52,11 @@ public abstract class InjectionConfig {
      */
     private List<FileOutConfig> fileOutConfigList;
 
+    /**
+     * 自定义判断是否创建文件
+     */
+    private IFileCreate fileCreate;
+
     /**
      * 注入自定义 Map 对象
      */

+ 44 - 0
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/IFileCreate.java

@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2011-2016, 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;
+
+
+import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
+import com.baomidou.mybatisplus.generator.config.po.FileType;
+
+/**
+ * <p>
+ * 文件覆盖接口
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-08-07
+ */
+public interface IFileCreate {
+
+    /**
+     * <p>
+     * 自定义判断是否创建文件
+     * </p>
+     *
+     * @param configBuilder 配置构建器
+     * @param fileType      文件类型
+     * @param filePath      文件路径
+     * @return
+     */
+    boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath);
+
+}

+ 4 - 2
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/builder/ConfigBuilder.java

@@ -431,7 +431,8 @@ public class ConfigBuilder {
             if (DbType.POSTGRE_SQL == dbQuery.dbType()) {
                 String schema = dataSourceConfig.getSchemaname();
                 if (schema == null) {
-                    schema = "public";//pg默认schema=public
+                    //pg默认schema=public
+                    schema = "public";
                     dataSourceConfig.setSchemaname(schema);
                 }
                 tablesSql = String.format(tablesSql, schema);
@@ -439,7 +440,8 @@ public class ConfigBuilder {
             //oracle数据库表太多,出现最大游标错误
             else if (DbType.ORACLE == dbQuery.dbType()) {
                 String schema = dataSourceConfig.getSchemaname();
-                if (schema == null) {//oracle默认用户的schema=username
+                //oracle默认用户的schema=username
+                if (schema == null) {
                     schema = dataSourceConfig.getUsername().toUpperCase();
                     dataSourceConfig.setSchemaname(schema);
                 }

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

@@ -0,0 +1,34 @@
+/*
+ * 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;
+
+/**
+ * <p>
+ * 生成文件类型
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-08-07
+ */
+public enum FileType {
+    ENTITY,
+    MAPPER,
+    XML,
+    SERVICE,
+    SERVICE_IMPL,
+    CONTROLLER,
+    OTHER;
+}

+ 16 - 8
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/engine/AbstractTemplateEngine.java

@@ -35,6 +35,7 @@ import com.baomidou.mybatisplus.generator.config.FileOutConfig;
 import com.baomidou.mybatisplus.generator.config.GlobalConfig;
 import com.baomidou.mybatisplus.generator.config.TemplateConfig;
 import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
+import com.baomidou.mybatisplus.generator.config.po.FileType;
 import com.baomidou.mybatisplus.generator.config.po.TableInfo;
 
 
@@ -86,7 +87,7 @@ public abstract class AbstractTemplateEngine {
                     List<FileOutConfig> focList = injectionConfig.getFileOutConfigList();
                     if (CollectionUtils.isNotEmpty(focList)) {
                         for (FileOutConfig foc : focList) {
-                            if (isCreate(foc.outputFile(tableInfo))) {
+                            if (isCreate(FileType.OTHER, foc.outputFile(tableInfo))) {
                                 writer(objectMap, foc.getTemplatePath(), foc.outputFile(tableInfo));
                             }
                         }
@@ -96,42 +97,42 @@ public abstract class AbstractTemplateEngine {
                 String entityName = tableInfo.getEntityName();
                 if (null != entityName && null != pathInfo.get(ConstVal.ENTITY_PATH)) {
                     String entityFile = String.format((pathInfo.get(ConstVal.ENTITY_PATH) + File.separator + "%s" + suffixJavaOrKt()), entityName);
-                    if (isCreate(entityFile)) {
+                    if (isCreate(FileType.ENTITY, entityFile)) {
                         writer(objectMap, templateFilePath(template.getEntity(getConfigBuilder().getGlobalConfig().isKotlin())), entityFile);
                     }
                 }
                 // MpMapper.java
                 if (null != tableInfo.getMapperName() && null != pathInfo.get(ConstVal.MAPPER_PATH)) {
                     String mapperFile = String.format((pathInfo.get(ConstVal.MAPPER_PATH) + File.separator + tableInfo.getMapperName() + suffixJavaOrKt()), entityName);
-                    if (isCreate(mapperFile)) {
+                    if (isCreate(FileType.MAPPER, mapperFile)) {
                         writer(objectMap, templateFilePath(template.getMapper()), mapperFile);
                     }
                 }
                 // MpMapper.xml
                 if (null != tableInfo.getXmlName() && null != pathInfo.get(ConstVal.XML_PATH)) {
                     String xmlFile = String.format((pathInfo.get(ConstVal.XML_PATH) + File.separator + tableInfo.getXmlName() + ConstVal.XML_SUFFIX), entityName);
-                    if (isCreate(xmlFile)) {
+                    if (isCreate(FileType.XML, xmlFile)) {
                         writer(objectMap, templateFilePath(template.getXml()), xmlFile);
                     }
                 }
                 // IMpService.java
                 if (null != tableInfo.getServiceName() && null != pathInfo.get(ConstVal.SERVICE_PATH)) {
                     String serviceFile = String.format((pathInfo.get(ConstVal.SERVICE_PATH) + File.separator + tableInfo.getServiceName() + suffixJavaOrKt()), entityName);
-                    if (isCreate(serviceFile)) {
+                    if (isCreate(FileType.SERVICE, serviceFile)) {
                         writer(objectMap, templateFilePath(template.getService()), serviceFile);
                     }
                 }
                 // MpServiceImpl.java
                 if (null != tableInfo.getServiceImplName() && null != pathInfo.get(ConstVal.SERVICE_IMPL_PATH)) {
                     String implFile = String.format((pathInfo.get(ConstVal.SERVICE_IMPL_PATH) + File.separator + tableInfo.getServiceImplName() + suffixJavaOrKt()), entityName);
-                    if (isCreate(implFile)) {
+                    if (isCreate(FileType.SERVICE_IMPL, implFile)) {
                         writer(objectMap, templateFilePath(template.getServiceImpl()), implFile);
                     }
                 }
                 // MpController.java
                 if (null != tableInfo.getControllerName() && null != pathInfo.get(ConstVal.CONTROLLER_PATH)) {
                     String controllerFile = String.format((pathInfo.get(ConstVal.CONTROLLER_PATH) + File.separator + tableInfo.getControllerName() + suffixJavaOrKt()), entityName);
-                    if (isCreate(controllerFile)) {
+                    if (isCreate(FileType.CONTROLLER, controllerFile)) {
                         writer(objectMap, templateFilePath(template.getController()), controllerFile);
                     }
                 }
@@ -279,7 +280,14 @@ public abstract class AbstractTemplateEngine {
      *
      * @return 是否
      */
-    protected boolean isCreate(String filePath) {
+    protected boolean isCreate(FileType fileType, String filePath) {
+        ConfigBuilder cb = getConfigBuilder();
+        // 自定义判断
+        InjectionConfig ic = cb.getInjectionConfig();
+        if (null != ic && null != ic.getFileCreate()) {
+            return ic.getFileCreate().isCreate(cb, fileType, filePath);
+        }
+        // 全局判断【默认】
         File file = new File(filePath);
         boolean exist = file.exists();
         if (!exist) {