From 5ea54ca1688796ca741bb4edaf77b5ad1005c9fe Mon Sep 17 00:00:00 2001 From: caoshd Date: Sun, 31 Mar 2024 11:50:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96:=20=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../caoshd/otone/builder/InfoOutBuilder.java | 8 +- .../caoshd/otone/builder/SourceBuilder.java | 156 +++++++++--------- .../space/caoshd/otone/util/SqlConsts.java | 2 +- 3 files changed, 84 insertions(+), 82 deletions(-) diff --git a/src/main/java/space/caoshd/otone/builder/InfoOutBuilder.java b/src/main/java/space/caoshd/otone/builder/InfoOutBuilder.java index c6ef616..b10b081 100644 --- a/src/main/java/space/caoshd/otone/builder/InfoOutBuilder.java +++ b/src/main/java/space/caoshd/otone/builder/InfoOutBuilder.java @@ -47,10 +47,10 @@ public class InfoOutBuilder { public InfoOutBuilder setClassName(String className) { String outputPath = CfgUtils.getJavaFilePath( - this.outputInfo.getPackagePath(), - className, - this.outputInfo.getNameSuffix(), - this.outputInfo.getNamePrefix() + this.outputInfo.getPackagePath(), + className, + this.outputInfo.getNameSuffix(), + this.outputInfo.getNamePrefix() ); this.outputInfo.setOutputPath(outputPath); return this; diff --git a/src/main/java/space/caoshd/otone/builder/SourceBuilder.java b/src/main/java/space/caoshd/otone/builder/SourceBuilder.java index 542046a..caff538 100644 --- a/src/main/java/space/caoshd/otone/builder/SourceBuilder.java +++ b/src/main/java/space/caoshd/otone/builder/SourceBuilder.java @@ -7,7 +7,10 @@ import space.caoshd.otone.entity.ContextInfo; import space.caoshd.otone.entity.OutputInfo; import space.caoshd.otone.entity.TableInfo; import space.caoshd.otone.tool.PropTools; -import space.caoshd.otone.util.*; +import space.caoshd.otone.util.CfgUtils; +import space.caoshd.otone.util.FileUtils; +import space.caoshd.otone.util.PathConsts; +import space.caoshd.otone.util.VelocityUtils; import java.util.Arrays; import java.util.Collections; @@ -20,16 +23,17 @@ public class SourceBuilder { private static final Logger LOGGER = LoggerFactory.getLogger(SourceBuilder.class); private static final PropTools CONFIG = new PropTools(PathConsts.CONFIG_PROPERTIES_PATH); - + private final Map extra = new HashMap<>(); private TableInfo tableInfo; - private OutputInfo outputInfo; - private List tableInfoList; - private List outputInfoList; - private final Map extra = new HashMap<>(); + private static ClassInfo buildClassInfo(TableInfo tableInfo, OutputInfo outputInfo) { + return new InfoClsBuilder().setTableName(tableInfo.getTableName()) + .setPackageName(outputInfo.getPackageName()) + .build(); + } public SourceBuilder setTableInfo(TableInfo tableInfo) { this.tableInfo = tableInfo; @@ -88,9 +92,9 @@ public class SourceBuilder { } private void build(TableInfo tableInfo, OutputInfo outputInfo) { + // 创建数据文件夹 FileUtils.mkdir(outputInfo.getPackagePath()); - // 创建上下文环境信息 ContextInfo contextInfo = new ContextInfo(); // 设置表信息 @@ -99,42 +103,40 @@ public class SourceBuilder { ClassInfo classInfo = buildClassInfo(tableInfo, outputInfo); contextInfo.setConfigInfo(classInfo); - // 获取输出文件路径 + // 完善输出信息 计算输出路径 enhanceOutputInfo(outputInfo, classInfo); - String outputPath = outputInfo.getOutputPath(); - // 获取模板文件路径 - String templatePath = outputInfo.getTemplatePath(); // 判断输出文件是否已存在 - if (!FileUtils.exists(outputPath)) { + if (!FileUtils.exists(outputInfo.getOutputPath())) { // 输出文件不存在 直接生成 - VelocityUtils.render(templatePath, outputPath, contextInfo); + VelocityUtils.render( + outputInfo.getTemplatePath(), + outputInfo.getOutputPath(), + contextInfo + ); } + // 输出文件存在 判断是否可以覆盖 if (CONFIG.getBoolean(CfgUtils.OUTPUT_COVER_IF_EXIST)) { // 配置可以执行覆盖 覆盖生成 - VelocityUtils.render(templatePath, outputPath, contextInfo); + VelocityUtils.render( + outputInfo.getTemplatePath(), + outputInfo.getOutputPath(), + contextInfo + ); } else { // 配置不可以执行覆盖 提示错误消息 - LOGGER.warn("output file already exists: {}, " + "please remove it or enable auto " + "cover exist file," - + " config.properties should like [output.cover_if_exists=true]", - outputPath + LOGGER.warn( + "output file already exists: {}, " + "please remove it or enable auto " + + "cover exist file," + + " config.properties should like [output.cover_if_exists=true]", + outputInfo.getOutputPath() ); } } private void enhanceOutputInfo(OutputInfo outputInfo, ClassInfo classInfo) { - new InfoOutBuilder() - .init(outputInfo) - .setClassName(classInfo.getClassName()) - .build(); - } - - private static ClassInfo buildClassInfo(TableInfo tableInfo, OutputInfo outputInfo) { - return new InfoClsBuilder() - .setTableName(tableInfo.getTableName()) - .setPackageName(outputInfo.getPackageName()) - .build(); + new InfoOutBuilder().init(outputInfo).setClassName(classInfo.getClassName()).build(); } private void build(TableInfo tableInfo, List outputInfoList) { @@ -147,55 +149,55 @@ public class SourceBuilder { private List defaultOutputInfoList() { return Arrays.asList( - // PO - new InfoOutBuilder().setNameSuffix(CfgUtils.getPoNameSuffix()) - .setTemplatePath(CfgUtils.getPoTemplatePath()) - .setPackagePath(CfgUtils.getPoPackagePath()) - .setPackageName(CfgUtils.getPoPackageName()) - .build(), - // MAPPER - new InfoOutBuilder().setNameSuffix(CfgUtils.getMapperNameSuffix()) - .setTemplatePath(CfgUtils.getMapperTemplatePath()) - .setPackagePath(CfgUtils.getMapperPackagePath()) - .setPackageName(CfgUtils.getMapperPackageName()) - .build(), - // SERVICE - new InfoOutBuilder().setNameSuffix(CfgUtils.getServiceNameSuffix()) - .setTemplatePath(CfgUtils.getServiceTemplatePath()) - .setPackagePath(CfgUtils.getServicePackagePath()) - .setPackageName(CfgUtils.getServicePackageName()) - .build(), - // SERVICE INTERFACE - new InfoOutBuilder().setNameSuffix(CfgUtils.getServiceNameSuffix()) - .setNamePrefix(CfgUtils.getServiceInterfacePrefix()) - .setTemplatePath(CfgUtils.getServiceInterfaceTemplatePath()) - .setPackagePath(CfgUtils.getServiceInterfacePackagePath()) - .setPackageName(CfgUtils.getServiceInterfacePackageName()) - .build(), - // CONTROLLER - new InfoOutBuilder().setNameSuffix(CfgUtils.getControllerNameSuffix()) - .setTemplatePath(CfgUtils.getControllerTemplatePath()) - .setPackagePath(CfgUtils.getControllerPackagePath()) - .setPackageName(CfgUtils.getControllerPackageName()) - .build(), - // VIEW - new InfoOutBuilder().setNameSuffix(CfgUtils.getViewNameSuffix()) - .setTemplatePath(CfgUtils.getViewTemplatePath()) - .setPackagePath(CfgUtils.getViewPackagePath()) - .setPackageName(CfgUtils.getViewPackageName()) - .build(), - // FORM - new InfoOutBuilder().setNameSuffix(CfgUtils.getFormNameSuffix()) - .setTemplatePath(CfgUtils.getFormTemplatePath()) - .setPackagePath(CfgUtils.getFormPackagePath()) - .setPackageName(CfgUtils.getFormPackageName()) - .build(), - // QUERY - new InfoOutBuilder().setNameSuffix(CfgUtils.getQueryNameSuffix()) - .setTemplatePath(CfgUtils.getQueryTemplatePath()) - .setPackagePath(CfgUtils.getQueryPackagePath()) - .setPackageName(CfgUtils.getQueryPackageName()) - .build() + // PO + new InfoOutBuilder().setNameSuffix(CfgUtils.getPoNameSuffix()) + .setTemplatePath(CfgUtils.getPoTemplatePath()) + .setPackagePath(CfgUtils.getPoPackagePath()) + .setPackageName(CfgUtils.getPoPackageName()) + .build(), + // MAPPER + new InfoOutBuilder().setNameSuffix(CfgUtils.getMapperNameSuffix()) + .setTemplatePath(CfgUtils.getMapperTemplatePath()) + .setPackagePath(CfgUtils.getMapperPackagePath()) + .setPackageName(CfgUtils.getMapperPackageName()) + .build(), + // SERVICE + new InfoOutBuilder().setNameSuffix(CfgUtils.getServiceNameSuffix()) + .setTemplatePath(CfgUtils.getServiceTemplatePath()) + .setPackagePath(CfgUtils.getServicePackagePath()) + .setPackageName(CfgUtils.getServicePackageName()) + .build(), + // SERVICE INTERFACE + new InfoOutBuilder().setNameSuffix(CfgUtils.getServiceNameSuffix()) + .setNamePrefix(CfgUtils.getServiceInterfacePrefix()) + .setTemplatePath(CfgUtils.getServiceInterfaceTemplatePath()) + .setPackagePath(CfgUtils.getServiceInterfacePackagePath()) + .setPackageName(CfgUtils.getServiceInterfacePackageName()) + .build(), + // CONTROLLER + new InfoOutBuilder().setNameSuffix(CfgUtils.getControllerNameSuffix()) + .setTemplatePath(CfgUtils.getControllerTemplatePath()) + .setPackagePath(CfgUtils.getControllerPackagePath()) + .setPackageName(CfgUtils.getControllerPackageName()) + .build(), + // VIEW + new InfoOutBuilder().setNameSuffix(CfgUtils.getViewNameSuffix()) + .setTemplatePath(CfgUtils.getViewTemplatePath()) + .setPackagePath(CfgUtils.getViewPackagePath()) + .setPackageName(CfgUtils.getViewPackageName()) + .build(), + // FORM + new InfoOutBuilder().setNameSuffix(CfgUtils.getFormNameSuffix()) + .setTemplatePath(CfgUtils.getFormTemplatePath()) + .setPackagePath(CfgUtils.getFormPackagePath()) + .setPackageName(CfgUtils.getFormPackageName()) + .build(), + // QUERY + new InfoOutBuilder().setNameSuffix(CfgUtils.getQueryNameSuffix()) + .setTemplatePath(CfgUtils.getQueryTemplatePath()) + .setPackagePath(CfgUtils.getQueryPackagePath()) + .setPackageName(CfgUtils.getQueryPackageName()) + .build() ); } diff --git a/src/main/java/space/caoshd/otone/util/SqlConsts.java b/src/main/java/space/caoshd/otone/util/SqlConsts.java index 76a6f9c..f55b7ca 100644 --- a/src/main/java/space/caoshd/otone/util/SqlConsts.java +++ b/src/main/java/space/caoshd/otone/util/SqlConsts.java @@ -1,7 +1,7 @@ package space.caoshd.otone.util; public class SqlConsts { - + public static final String TABLE_SQL = "table.sql"; public static final String TABLE_SQL_SCHEMA = "table.sql.schema"; public static final String TABLE_SQL_SCHEMA_TABLE = "table.sql.schema.table";