修改: 分离表信息和类信息

master
曹世达 6 months ago
parent f16f2642e6
commit e593d2415b

@ -0,0 +1,49 @@
package space.caoshd.otone.builder;
import space.caoshd.otone.entity.ClassInfo;
import space.caoshd.otone.tool.PropTools;
import space.caoshd.otone.util.CfgUtils;
import space.caoshd.otone.util.PathConsts;
import space.caoshd.otone.util.StrUtils;
import java.util.List;
public class InfoClsBuilder {
private static final PropTools CONFIG = new PropTools(PathConsts.CONFIG_PROPERTIES_PATH);
private final ClassInfo classInfo = new ClassInfo();
private String getTableNameWithoutPrefix(String tableNameWithoutPrefix) {
List<String> tablePrefixes = CONFIG.getStrings(CfgUtils.DATABASE_TABLE_PREFIXES);
for (String tablePrefix : tablePrefixes) {
tableNameWithoutPrefix = StrUtils.removePrefix(tableNameWithoutPrefix, tablePrefix);
}
return tableNameWithoutPrefix;
}
public InfoClsBuilder setTableName(String tableName) {
String tableNameWithoutPrefix = getTableNameWithoutPrefix(tableName);
String className = StrUtils.toCamelCase(tableNameWithoutPrefix, true);
this.classInfo.setClassName(className);
return this;
}
public InfoClsBuilder setPackageName(String packageName) {
this.classInfo.setPackageName(packageName);
return this;
}
public ClassInfo build() {
classInfo.setControllerSuffix(CfgUtils.getControllerNameSuffix());
classInfo.setFormSuffix(CfgUtils.getFormNameSuffix());
classInfo.setQuerySuffix(CfgUtils.getQueryNameSuffix());
classInfo.setViewSuffix(CfgUtils.getViewNameSuffix());
classInfo.setServiceSuffix(CfgUtils.getServiceNameSuffix());
classInfo.setServieInterfacePrefix(CfgUtils.getServiceInterfacePrefix());
classInfo.setMapperSuffix(CfgUtils.getMapperNameSuffix());
classInfo.setPoSuffix(CfgUtils.getPoNameSuffix());
return classInfo;
}
}

@ -0,0 +1,64 @@
package space.caoshd.otone.builder;
import space.caoshd.otone.entity.OutputInfo;
import space.caoshd.otone.entity.TableInfo;
import space.caoshd.otone.util.CfgUtils;
import java.util.List;
public class InfoOutBuilder {
private OutputInfo outputInfo = new OutputInfo();
public InfoOutBuilder init(OutputInfo outputInfo) {
this.outputInfo = outputInfo;
return this;
}
public InfoOutBuilder setTableInfoList(List<TableInfo> tableInfoList) {
this.outputInfo.setTableInfoList(tableInfoList);
return this;
}
public InfoOutBuilder setTemplatePath(String templatePath) {
this.outputInfo.setTemplatePath(templatePath);
return this;
}
public InfoOutBuilder setPackagePath(String packagePath) {
this.outputInfo.setPackagePath(packagePath);
return this;
}
public InfoOutBuilder setNameSuffix(String nameSuffix) {
this.outputInfo.setNameSuffix(nameSuffix);
return this;
}
public InfoOutBuilder setNamePrefix(String namePrefix) {
this.outputInfo.setNamePrefix(namePrefix);
return this;
}
public InfoOutBuilder setPackageName(String packageName) {
this.outputInfo.setPackageName(packageName);
return this;
}
public InfoOutBuilder setClassName(String className) {
String outputPath = CfgUtils.getJavaFilePath(
this.outputInfo.getPackagePath(),
className,
this.outputInfo.getNameSuffix(),
this.outputInfo.getNamePrefix()
);
this.outputInfo.setOutputPath(outputPath);
return this;
}
public OutputInfo build() {
return this.outputInfo;
}
}

@ -1,47 +0,0 @@
package space.caoshd.otone.builder;
import space.caoshd.otone.entity.OutputInfo;
import space.caoshd.otone.entity.TableInfo;
import java.util.List;
public class OutputBuilder {
private final OutputInfo outputInfo = new OutputInfo();
public OutputBuilder setTableInfoList(List<TableInfo> tableInfoList) {
this.outputInfo.setTableInfoList(tableInfoList);
return this;
}
public OutputBuilder setTemplatePath(String templatePath) {
this.outputInfo.setTemplatePath(templatePath);
return this;
}
public OutputBuilder setPackagePath(String packagePath) {
this.outputInfo.setPackagePath(packagePath);
return this;
}
public OutputBuilder setNameSuffix(String nameSuffix) {
this.outputInfo.setNameSuffix(nameSuffix);
return this;
}
public OutputBuilder setNamePrefix(String namePrefix) {
this.outputInfo.setNamePrefix(namePrefix);
return this;
}
public OutputBuilder setPackageName(String packageName) {
this.outputInfo.setPackageName(packageName);
return this;
}
public OutputInfo build() {
return outputInfo;
}
}

@ -2,13 +2,12 @@ package space.caoshd.otone.builder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import space.caoshd.otone.entity.ClassInfo;
import space.caoshd.otone.entity.ContextInfo;
import space.caoshd.otone.entity.OutputInfo; import space.caoshd.otone.entity.OutputInfo;
import space.caoshd.otone.entity.TableInfo; import space.caoshd.otone.entity.TableInfo;
import space.caoshd.otone.tool.PropTools; import space.caoshd.otone.tool.PropTools;
import space.caoshd.otone.util.CfgUtils; import space.caoshd.otone.util.*;
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.Arrays;
import java.util.Collections; import java.util.Collections;
@ -89,43 +88,55 @@ public class SourceBuilder {
} }
private void build(TableInfo tableInfo, OutputInfo outputInfo) { private void build(TableInfo tableInfo, OutputInfo outputInfo) {
// 获取表信息 配置信息
String beanName = tableInfo.getClassName();
String nameSuffix = outputInfo.getNameSuffix();
String namePrefix = outputInfo.getNamePrefix();
String packagePath = outputInfo.getPackagePath();
String packageName = outputInfo.getPackageName();
// 添加附加信息
tableInfo.setExtra(extra);
tableInfo.setClassNameSuffix(nameSuffix);
tableInfo.setClassNamePrefix(namePrefix);
tableInfo.setPackageName(packageName);
// 创建数据文件夹 // 创建数据文件夹
FileUtils.mkdir(packagePath); FileUtils.mkdir(outputInfo.getPackagePath());
// 创建上下文环境信息
ContextInfo contextInfo = new ContextInfo();
// 设置表信息
contextInfo.setTableInfo(tableInfo);
// 设置类信息
ClassInfo classInfo = buildClassInfo(tableInfo, outputInfo);
contextInfo.setConfigInfo(classInfo);
// 获取输出文件路径 // 获取输出文件路径
String outputPath = CfgUtils.getJavaFilePath(packagePath, beanName, nameSuffix, namePrefix); enhanceOutputInfo(outputInfo, classInfo);
String outputPath = outputInfo.getOutputPath();
// 获取模板文件路径 // 获取模板文件路径
String templatePath = outputInfo.getTemplatePath(); String templatePath = outputInfo.getTemplatePath();
// 判断输出文件是否已存在 // 判断输出文件是否已存在
if (!FileUtils.exists(outputPath)) { if (!FileUtils.exists(outputPath)) {
// 输出文件不存在 直接生成 // 输出文件不存在 直接生成
VelocityUtils.render(CfgUtils.VALUE_TABLE, tableInfo, templatePath, outputPath); VelocityUtils.render(templatePath, outputPath, contextInfo);
} }
// 输出文件存在 判断是否可以覆盖 // 输出文件存在 判断是否可以覆盖
if (CONFIG.getBoolean(CfgUtils.OUTPUT_COVER_IF_EXIST)) { if (CONFIG.getBoolean(CfgUtils.OUTPUT_COVER_IF_EXIST)) {
// 配置可以执行覆盖 覆盖生成 // 配置可以执行覆盖 覆盖生成
VelocityUtils.render(CfgUtils.VALUE_TABLE, tableInfo, templatePath, outputPath); VelocityUtils.render(templatePath, outputPath, contextInfo);
} else { } else {
// 配置不可以执行覆盖 提示错误消息 // 配置不可以执行覆盖 提示错误消息
LOGGER.warn("output file already exists: {}, " + "please remove it or enable auto " + "cover exist file," LOGGER.warn("output file already exists: {}, " + "please remove it or enable auto " + "cover exist file,"
+ " config.properties should like [output.cover_if_exists=true]", + " config.properties should like [output.cover_if_exists=true]",
outputPath outputPath
); );
} }
} }
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();
}
private void build(TableInfo tableInfo, List<OutputInfo> outputInfoList) { private void build(TableInfo tableInfo, List<OutputInfo> outputInfoList) {
// 循环输出信息 // 循环输出信息
for (OutputInfo outputInfo : outputInfoList) { for (OutputInfo outputInfo : outputInfoList) {
@ -136,55 +147,55 @@ public class SourceBuilder {
private List<OutputInfo> defaultOutputInfoList() { private List<OutputInfo> defaultOutputInfoList() {
return Arrays.asList( return Arrays.asList(
// PO // PO
new OutputBuilder().setNameSuffix(CfgUtils.getPoNameSuffix()) new InfoOutBuilder().setNameSuffix(CfgUtils.getPoNameSuffix())
.setTemplatePath(CfgUtils.getPoTemplatePath()) .setTemplatePath(CfgUtils.getPoTemplatePath())
.setPackagePath(CfgUtils.getPoPackagePath()) .setPackagePath(CfgUtils.getPoPackagePath())
.setPackageName(CfgUtils.getPoPackageName()) .setPackageName(CfgUtils.getPoPackageName())
.build(), .build(),
// MAPPER // MAPPER
new OutputBuilder().setNameSuffix(CfgUtils.getMapperNameSuffix()) new InfoOutBuilder().setNameSuffix(CfgUtils.getMapperNameSuffix())
.setTemplatePath(CfgUtils.getMapperTemplatePath()) .setTemplatePath(CfgUtils.getMapperTemplatePath())
.setPackagePath(CfgUtils.getMapperPackagePath()) .setPackagePath(CfgUtils.getMapperPackagePath())
.setPackageName(CfgUtils.getMapperPackageName()) .setPackageName(CfgUtils.getMapperPackageName())
.build(), .build(),
// SERVICE // SERVICE
new OutputBuilder().setNameSuffix(CfgUtils.getServiceNameSuffix()) new InfoOutBuilder().setNameSuffix(CfgUtils.getServiceNameSuffix())
.setTemplatePath(CfgUtils.getServiceTemplatePath()) .setTemplatePath(CfgUtils.getServiceTemplatePath())
.setPackagePath(CfgUtils.getServicePackagePath()) .setPackagePath(CfgUtils.getServicePackagePath())
.setPackageName(CfgUtils.getServicePackageName()) .setPackageName(CfgUtils.getServicePackageName())
.build(), .build(),
// SERVICE INTERFACE // SERVICE INTERFACE
new OutputBuilder().setNameSuffix(CfgUtils.getServiceNameSuffix()) new InfoOutBuilder().setNameSuffix(CfgUtils.getServiceNameSuffix())
.setNamePrefix(CfgUtils.getServiceInterfacePrefix()) .setNamePrefix(CfgUtils.getServiceInterfacePrefix())
.setTemplatePath(CfgUtils.getServiceInterfaceTemplatePath()) .setTemplatePath(CfgUtils.getServiceInterfaceTemplatePath())
.setPackagePath(CfgUtils.getServiceInterfacePackagePath()) .setPackagePath(CfgUtils.getServiceInterfacePackagePath())
.setPackageName(CfgUtils.getServiceInterfacePackageName()) .setPackageName(CfgUtils.getServiceInterfacePackageName())
.build(), .build(),
// CONTROLLER // CONTROLLER
new OutputBuilder().setNameSuffix(CfgUtils.getControllerNameSuffix()) new InfoOutBuilder().setNameSuffix(CfgUtils.getControllerNameSuffix())
.setTemplatePath(CfgUtils.getControllerTemplatePath()) .setTemplatePath(CfgUtils.getControllerTemplatePath())
.setPackagePath(CfgUtils.getControllerPackagePath()) .setPackagePath(CfgUtils.getControllerPackagePath())
.setPackageName(CfgUtils.getControllerPackageName()) .setPackageName(CfgUtils.getControllerPackageName())
.build(), .build(),
// VIEW // VIEW
new OutputBuilder().setNameSuffix(CfgUtils.getViewNameSuffix()) new InfoOutBuilder().setNameSuffix(CfgUtils.getViewNameSuffix())
.setTemplatePath(CfgUtils.getViewTemplatePath()) .setTemplatePath(CfgUtils.getViewTemplatePath())
.setPackagePath(CfgUtils.getViewPackagePath()) .setPackagePath(CfgUtils.getViewPackagePath())
.setPackageName(CfgUtils.getViewPackageName()) .setPackageName(CfgUtils.getViewPackageName())
.build(), .build(),
// FORM // FORM
new OutputBuilder().setNameSuffix(CfgUtils.getFormNameSuffix()) new InfoOutBuilder().setNameSuffix(CfgUtils.getFormNameSuffix())
.setTemplatePath(CfgUtils.getFormTemplatePath()) .setTemplatePath(CfgUtils.getFormTemplatePath())
.setPackagePath(CfgUtils.getFormPackagePath()) .setPackagePath(CfgUtils.getFormPackagePath())
.setPackageName(CfgUtils.getFormPackageName()) .setPackageName(CfgUtils.getFormPackageName())
.build(), .build(),
// QUERY // QUERY
new OutputBuilder().setNameSuffix(CfgUtils.getQueryNameSuffix()) new InfoOutBuilder().setNameSuffix(CfgUtils.getQueryNameSuffix())
.setTemplatePath(CfgUtils.getQueryTemplatePath()) .setTemplatePath(CfgUtils.getQueryTemplatePath())
.setPackagePath(CfgUtils.getQueryPackagePath()) .setPackagePath(CfgUtils.getQueryPackagePath())
.setPackageName(CfgUtils.getQueryPackageName()) .setPackageName(CfgUtils.getQueryPackageName())
.build() .build()
); );
} }

@ -11,7 +11,6 @@ import space.caoshd.otone.util.DBUtils;
import space.caoshd.otone.util.JsonUtils; import space.caoshd.otone.util.JsonUtils;
import space.caoshd.otone.util.PathConsts; import space.caoshd.otone.util.PathConsts;
import space.caoshd.otone.util.SqlConsts; import space.caoshd.otone.util.SqlConsts;
import space.caoshd.otone.util.StrUtils;
import space.caoshd.otone.util.TypeUtils; import space.caoshd.otone.util.TypeUtils;
import java.util.ArrayList; import java.util.ArrayList;
@ -95,10 +94,6 @@ public class TableBuilder {
String tableName = table.get(tableNameLabel); String tableName = table.get(tableNameLabel);
result.setTableName(tableName); result.setTableName(tableName);
String tableNameWithoutPrefix = getTableNameWithoutPrefix(result.getTableName());
String beanName = StrUtils.toCamelCase(tableNameWithoutPrefix, true);
result.setClassName(beanName);
List<ColumnInfo> columnInfo = loadColumnInfo(tableSchema, tableName); List<ColumnInfo> columnInfo = loadColumnInfo(tableSchema, tableName);
result.setColumns(columnInfo); result.setColumns(columnInfo);
@ -145,13 +140,4 @@ public class TableBuilder {
.orElseThrow(() -> new RuntimeException("column: " + columnName + " not exists.")); .orElseThrow(() -> new RuntimeException("column: " + columnName + " not exists."));
} }
private String getTableNameWithoutPrefix(String tableNameWithoutPrefix) {
List<String> tablePrefixes = CONFIG.getStrings(CfgUtils.DATABASE_TABLE_PREFIXES);
for (String tablePrefix : tablePrefixes) {
tableNameWithoutPrefix = StrUtils.removePrefix(tableNameWithoutPrefix, tablePrefix);
}
return tableNameWithoutPrefix;
}
} }

@ -0,0 +1,104 @@
package space.caoshd.otone.entity;
public class ClassInfo {
private String packageName;
private String className;
private String controllerSuffix;
private String serviceSuffix;
private String servieInterfacePrefix;
private String mapperSuffix;
private String poSuffix;
private String viewSuffix;
private String formSuffix;
private String querySuffix;
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getControllerSuffix() {
return controllerSuffix;
}
public void setControllerSuffix(String controllerSuffix) {
this.controllerSuffix = controllerSuffix;
}
public String getServiceSuffix() {
return serviceSuffix;
}
public void setServiceSuffix(String serviceSuffix) {
this.serviceSuffix = serviceSuffix;
}
public String getServieInterfacePrefix() {
return servieInterfacePrefix;
}
public void setServieInterfacePrefix(String servieInterfacePrefix) {
this.servieInterfacePrefix = servieInterfacePrefix;
}
public String getMapperSuffix() {
return mapperSuffix;
}
public void setMapperSuffix(String mapperSuffix) {
this.mapperSuffix = mapperSuffix;
}
public String getPoSuffix() {
return poSuffix;
}
public void setPoSuffix(String poSuffix) {
this.poSuffix = poSuffix;
}
public String getViewSuffix() {
return viewSuffix;
}
public void setViewSuffix(String viewSuffix) {
this.viewSuffix = viewSuffix;
}
public String getFormSuffix() {
return formSuffix;
}
public void setFormSuffix(String formSuffix) {
this.formSuffix = formSuffix;
}
public String getQuerySuffix() {
return querySuffix;
}
public void setQuerySuffix(String querySuffix) {
this.querySuffix = querySuffix;
}
}

@ -0,0 +1,24 @@
package space.caoshd.otone.entity;
public class ContextInfo {
private TableInfo tableInfo;
private ClassInfo classInfo;
public TableInfo getTableInfo() {
return tableInfo;
}
public void setTableInfo(TableInfo tableInfo) {
this.tableInfo = tableInfo;
}
public ClassInfo getConfigInfo() {
return classInfo;
}
public void setConfigInfo(ClassInfo classInfo) {
this.classInfo = classInfo;
}
}

@ -12,6 +12,8 @@ public class OutputInfo {
private String packagePath; private String packagePath;
private String outputPath;
private String nameSuffix = ""; private String nameSuffix = "";
private String namePrefix = ""; private String namePrefix = "";
@ -63,4 +65,12 @@ public class OutputInfo {
public void setNamePrefix(String namePrefix) { public void setNamePrefix(String namePrefix) {
this.namePrefix = namePrefix; this.namePrefix = namePrefix;
} }
public String getOutputPath() {
return outputPath;
}
public void setOutputPath(String outputPath) {
this.outputPath = outputPath;
}
} }

@ -1,7 +1,6 @@
package space.caoshd.otone.entity; package space.caoshd.otone.entity;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -11,15 +10,10 @@ public class TableInfo {
private String comment; private String comment;
private String tableSchema; private String tableSchema;
private String tableName; private String tableName;
private String packageName;
private String className;
private String classNameSuffix;
private String classNamePrefix;
private Boolean dateExists; private Boolean dateExists;
private Boolean decimalExists; private Boolean decimalExists;
private List<ColumnInfo> columns = new ArrayList<>(); private List<ColumnInfo> columns = new ArrayList<>();
private Map<String, List<ColumnInfo>> indexes = new LinkedHashMap<>(); private Map<String, List<ColumnInfo>> indexes = new LinkedHashMap<>();
private Map<String, Object> extra = new HashMap<>();
public String getComment() { public String getComment() {
return comment; return comment;
@ -45,38 +39,6 @@ public class TableInfo {
this.tableName = tableName; this.tableName = tableName;
} }
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getClassNamePrefix() {
return classNamePrefix;
}
public void setClassNamePrefix(String classNamePrefix) {
this.classNamePrefix = classNamePrefix;
}
public String getClassNameSuffix() {
return classNameSuffix;
}
public void setClassNameSuffix(String classNameSuffix) {
this.classNameSuffix = classNameSuffix;
}
public Boolean getDateExists() { public Boolean getDateExists() {
return dateExists; return dateExists;
} }
@ -109,11 +71,4 @@ public class TableInfo {
this.indexes = indexes; this.indexes = indexes;
} }
public Map<String, Object> getExtra() {
return extra;
}
public void setExtra(Map<String, Object> extra) {
this.extra = extra;
}
} }

@ -6,6 +6,7 @@ import org.apache.velocity.app.Velocity;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import space.caoshd.otone.builder.TableBuilder; import space.caoshd.otone.builder.TableBuilder;
import space.caoshd.otone.entity.ContextInfo;
import space.caoshd.otone.tool.PropTools; import space.caoshd.otone.tool.PropTools;
import java.io.FileWriter; import java.io.FileWriter;
@ -17,24 +18,25 @@ public class VelocityUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(TableBuilder.class); private static final Logger LOGGER = LoggerFactory.getLogger(TableBuilder.class);
public static void render( public static void render(String templatePath, String outputPath, ContextInfo ctx) {
String beanName, Object bean, String templatePath, String outputPath
) {
PropTools propTools = new PropTools(PathConsts.VELOCITY_PROPERTIES_PATH); PropTools propTools = new PropTools(PathConsts.VELOCITY_PROPERTIES_PATH);
Properties properties = propTools.getProperties(); Properties properties = propTools.getProperties();
Velocity.init(properties); Velocity.init(properties);
VelocityContext context = new VelocityContext(); VelocityContext context = new VelocityContext();
context.put("context", bean);
context.put("tbl", ctx.getTableInfo());
context.put("cfg", ctx.getConfigInfo());
Template template = Velocity.getTemplate(templatePath, StandardCharsets.UTF_8.name()); Template template = Velocity.getTemplate(templatePath, StandardCharsets.UTF_8.name());
try (FileWriter fw = new FileWriter(outputPath)) { try (FileWriter fw = new FileWriter(outputPath)) {
LOGGER.info("generate file: {}", outputPath); LOGGER.info("generate file: {}", outputPath);
LOGGER.info("context info: {}", JsonUtils.toJson(bean)); LOGGER.info("context info: {}", JsonUtils.toJson(ctx));
template.merge(context, fw); template.merge(context, fw);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
} }

@ -15,6 +15,7 @@ package.name.service.interface=service
#module.name.controller=controller #module.name.controller=controller
#module.name.repository=data #module.name.repository=data
#module.name.service=cmpt #module.name.service=cmpt
#java.name.prefix.service=I
#java.name.suffix.service=Service #java.name.suffix.service=Service
#java.name.suffix.controller=Controller #java.name.suffix.controller=Controller
#java.name.suffix.view=View #java.name.suffix.view=View

Loading…
Cancel
Save