fix:web端代码生成,参数未完全替换的bug

This commit is contained in:
zhangcheng 2023-05-22 22:23:12 +08:00
parent 6cedd28256
commit 76706d67a6
4 changed files with 56 additions and 54 deletions

View File

@ -184,26 +184,57 @@ 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);
setSubTable(table); for (String template : result.templates) {
// 设置主键列信息 if (template.endsWith(".java.vm")) {
setPkColumn(table); result.context.put("fullPackage", getFullPackage(template));
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(context, sw); tpl.merge(result.context, sw);
dataMap.put(template, sw.toString()); dataMap.put(template, sw.toString());
} }
return dataMap; return dataMap;
} }
private Result getResult(GenTable table) {
initNullValue(table);
// 设置主子表信息
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());
// 初始化模板
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());
Result result = new Result(context, templates);
return result;
}
private static class Result {
public final VelocityContext context;
public final List<String> templates;
public Result(VelocityContext context, List<String> templates) {
this.context = context;
this.templates = templates;
}
}
/** /**
* 生成代码下载方式 * 生成代码下载方式
* *
@ -228,37 +259,15 @@ 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);
initNullValue(table); Result result = getResult(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());
// 初始化模板
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")) {
context.put("fullPackage", getFullPackage(template)); result.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(context, sw); tpl.merge(result.context, sw);
String path = null; String path = null;
try { try {
path = generatePath(template, table); path = generatePath(template, table);
@ -389,22 +398,15 @@ 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);
setSubTable(table); for (String template : result.templates) {
// 设置主键列信息 if (template.endsWith(".java.vm")) {
setPkColumn(table); result.context.put("fullPackage", getFullPackage(template));
}
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(context, sw); tpl.merge(result.context, sw);
try { try {
// 添加到zip // 添加到zip
zip.putNextEntry(new ZipEntry(VelocityUtils.getFileName(template, table))); zip.putNextEntry(new ZipEntry(VelocityUtils.getFileName(template, table)));

View File

@ -29,7 +29,7 @@ import ${_fullClass.vo};
import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.common.utils.poi.ExcelUtil;
/** /**
* ${functionName}Controller * ${functionName}Controller
* *
* @author ${author} * @author ${author}
* @date ${datetime} * @date ${datetime}
*/ */

View File

@ -38,7 +38,7 @@ public class ${ClassName}Service {
* @return ${functionName} * @return ${functionName}
*/ */
public ${ClassName} selectBy${pkColumn.capJavaField}(${pkColumn.javaType} ${pkColumn.javaField}) { public ${ClassName} selectBy${pkColumn.capJavaField}(${pkColumn.javaType} ${pkColumn.javaField}) {
return ${className}Mapper.selectBy${pkColumn.capJavaField}(${pkColumn.javaField}); return ${className}Mapper.selectById(${pkColumn.javaField});
} }
/** /**

View File

@ -38,7 +38,7 @@ public class ${ClassName}Service {
* @return ${functionName} * @return ${functionName}
*/ */
public ${ClassName} selectBy${pkColumn.capJavaField}(${pkColumn.javaType} ${pkColumn.javaField}) { public ${ClassName} selectBy${pkColumn.capJavaField}(${pkColumn.javaType} ${pkColumn.javaField}) {
return ${className}Mapper.selectBy${pkColumn.capJavaField}(${pkColumn.javaField}); return ${className}Mapper.selectById(${pkColumn.javaField});
} }
/** /**