From 7a10ce49f52412c25ace607c8aca7107a9e1c191 Mon Sep 17 00:00:00 2001 From: zccbbg Date: Sat, 2 Mar 2024 21:25:03 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E4=BB=A3=E7=A0=81=E7=94=9F?= =?UTF-8?q?=E6=88=90bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/GenTableServiceImpl.java | 97 +++++++++---------- 1 file changed, 48 insertions(+), 49 deletions(-) diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java index 8a21667..30a7c8c 100644 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java +++ b/ruoyi-generator/src/main/java/com/ruoyi/generator/service/GenTableServiceImpl.java @@ -184,55 +184,24 @@ public class GenTableServiceImpl implements IGenTableService { Map dataMap = new LinkedHashMap<>(); // 查询表信息 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); - 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 templates = VelocityUtils.getTemplateList(table.getTplCategory()); - Result result = new Result(context, templates); - return result; - } - - private static class Result { - public final VelocityContext context; - public final List templates; - - public Result(VelocityContext context, List templates) { - this.context = context; - this.templates = templates; + for (String template : templates) { + // 渲染模板 + StringWriter sw = new StringWriter(); + Template tpl = Velocity.getTemplate(template, Constants.UTF8); + tpl.merge(context, sw); + dataMap.put(template, sw.toString()); } + return dataMap; } /** @@ -259,15 +228,38 @@ public class GenTableServiceImpl implements IGenTableService { public void generatorCode(String tableName) { // 查询表信息 GenTable table = genTableMapper.selectGenTableByName(tableName); - Result result = getResult(table); - for (String template : result.templates) { + 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()); + 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 templates = VelocityUtils.getTemplateList(table.getTplCategory()); + for (String template : templates) { if (template.endsWith(".java.vm")) { - result.context.put("fullPackage", getFullPackage(template)); + context.put("fullPackage", getFullPackage(template)); } // 渲染模板 StringWriter sw = new StringWriter(); Template tpl = Velocity.getTemplate(template, Constants.UTF8); - tpl.merge(result.context, sw); + tpl.merge(context, sw); String path = null; try { path = generatePath(template, table); @@ -398,15 +390,22 @@ public class GenTableServiceImpl implements IGenTableService { private void generatorCode(String tableName, ZipOutputStream zip) { // 查询表信息 GenTable table = genTableMapper.selectGenTableByName(tableName); - Result result = getResult(table); - for (String template : result.templates) { - if (template.endsWith(".java.vm")) { - result.context.put("fullPackage", getFullPackage(template)); - } + // 设置主子表信息 + setSubTable(table); + // 设置主键列信息 + setPkColumn(table); + + VelocityInitializer.initVelocity(); + + VelocityContext context = VelocityUtils.prepareContext(table); + + // 获取模板列表 + List templates = VelocityUtils.getTemplateList(table.getTplCategory()); + for (String template : templates) { // 渲染模板 StringWriter sw = new StringWriter(); Template tpl = Velocity.getTemplate(template, Constants.UTF8); - tpl.merge(result.context, sw); + tpl.merge(context, sw); try { // 添加到zip zip.putNextEntry(new ZipEntry(VelocityUtils.getFileName(template, table)));