解决代码生成bug

pull/1/head
zccbbg 2 years ago
parent 5f6af3ec90
commit 7a10ce49f5

@ -184,55 +184,24 @@ public class GenTableServiceImpl implements IGenTableService {
Map<String, String> dataMap = new LinkedHashMap<>(); Map<String, String> dataMap = new LinkedHashMap<>();
// 查询表信息 // 查询表信息
GenTable table = genTableMapper.selectGenTableById(tableId); GenTable table = genTableMapper.selectGenTableById(tableId);
Result result = getResult(table);
for (String template : result.templates) {
if (template.endsWith(".java.vm")) {
result.context.put("fullPackage", getFullPackage(template));
}
// 渲染模板
StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, Constants.UTF8);
tpl.merge(result.context, sw);
dataMap.put(template, sw.toString());
}
return dataMap;
}
private Result getResult(GenTable table) {
initNullValue(table);
// 设置主子表信息 // 设置主子表信息
setSubTable(table); setSubTable(table);
setTableFromOptions(table);
// 设置主键列信息 // 设置主键列信息
setPkColumn(table); setPkColumn(table);
// 初始化 Class 信息
genContext.prop2path.put("ClassName", table.getClassName());
genContext.prop2path.put("className", StrUtil.lowerFirst(table.getClassName()));
genContext.prop2path.put("tableName", table.getTableName());
// 初始化模板
VelocityInitializer.initVelocity(); VelocityInitializer.initVelocity();
// 初始化模板变量
VelocityContext context = VelocityUtils.prepareContext(table); VelocityContext context = VelocityUtils.prepareContext(table);
context.put("env", genContext.genConfig.getEnv());
context.put("_fullClass", fillHolder(table, genContext.fullQualifiedClassHolder));
context.put("_className", fillHolder(table, genContext.className));
// 获取模板列表 // 获取模板列表
List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory()); List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
Result result = new Result(context, templates); for (String template : templates) {
return result; // 渲染模板
} StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, Constants.UTF8);
private static class Result { tpl.merge(context, sw);
public final VelocityContext context; dataMap.put(template, sw.toString());
public final List<String> templates;
public Result(VelocityContext context, List<String> templates) {
this.context = context;
this.templates = templates;
} }
return dataMap;
} }
/** /**
@ -259,15 +228,38 @@ public class GenTableServiceImpl implements IGenTableService {
public void generatorCode(String tableName) { public void generatorCode(String tableName) {
// 查询表信息 // 查询表信息
GenTable table = genTableMapper.selectGenTableByName(tableName); GenTable table = genTableMapper.selectGenTableByName(tableName);
Result result = getResult(table); initNullValue(table);
for (String template : result.templates) { // 设置主子表信息
setSubTable(table);
setTableFromOptions(table);
// 设置主键列信息
setPkColumn(table);
// 初始化 Class 信息
genContext.prop2path.put("ClassName", table.getClassName());
genContext.prop2path.put("className", StrUtil.lowerFirst(table.getClassName()));
genContext.prop2path.put("tableName", table.getTableName());
genContext.prop2path.put("moduleName", table.getModuleName());
// 初始化模板
VelocityInitializer.initVelocity();
// 初始化模板变量
VelocityContext context = VelocityUtils.prepareContext(table);
context.put("env", genContext.genConfig.getEnv());
context.put("_fullClass", fillHolder(table, genContext.fullQualifiedClassHolder));
context.put("_className", fillHolder(table, genContext.className));
// 获取模板列表
List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
for (String template : templates) {
if (template.endsWith(".java.vm")) { if (template.endsWith(".java.vm")) {
result.context.put("fullPackage", getFullPackage(template)); context.put("fullPackage", getFullPackage(template));
} }
// 渲染模板 // 渲染模板
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, Constants.UTF8); Template tpl = Velocity.getTemplate(template, Constants.UTF8);
tpl.merge(result.context, sw); tpl.merge(context, sw);
String path = null; String path = null;
try { try {
path = generatePath(template, table); path = generatePath(template, table);
@ -398,15 +390,22 @@ public class GenTableServiceImpl implements IGenTableService {
private void generatorCode(String tableName, ZipOutputStream zip) { private void generatorCode(String tableName, ZipOutputStream zip) {
// 查询表信息 // 查询表信息
GenTable table = genTableMapper.selectGenTableByName(tableName); GenTable table = genTableMapper.selectGenTableByName(tableName);
Result result = getResult(table); // 设置主子表信息
for (String template : result.templates) { setSubTable(table);
if (template.endsWith(".java.vm")) { // 设置主键列信息
result.context.put("fullPackage", getFullPackage(template)); setPkColumn(table);
}
VelocityInitializer.initVelocity();
VelocityContext context = VelocityUtils.prepareContext(table);
// 获取模板列表
List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
for (String template : templates) {
// 渲染模板 // 渲染模板
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, Constants.UTF8); Template tpl = Velocity.getTemplate(template, Constants.UTF8);
tpl.merge(result.context, sw); tpl.merge(context, sw);
try { try {
// 添加到zip // 添加到zip
zip.putNextEntry(new ZipEntry(VelocityUtils.getFileName(template, table))); zip.putNextEntry(new ZipEntry(VelocityUtils.getFileName(template, table)));

Loading…
Cancel
Save