From 89bf5bf10a88cdf0ef0758f5725a0c7eae7f5de1 Mon Sep 17 00:00:00 2001 From: weihongyang <1075331873@qq.com> Date: Wed, 22 Jun 2022 14:02:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E4=B8=80=E4=B8=AAswa?= =?UTF-8?q?gger=E8=87=AA=E5=8A=A8=E7=94=9F=E6=88=90=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=9A=84=E6=A8=A1=E6=9D=BF=E5=BC=95=E6=93=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cnbm/generator/build/CodeGenerator.java | 84 ++----------------- .../engine/EnhanceVelocityTemplateEngine.java | 7 ++ 2 files changed, 13 insertions(+), 78 deletions(-) diff --git a/ym-generator/src/main/java/com/cnbm/generator/build/CodeGenerator.java b/ym-generator/src/main/java/com/cnbm/generator/build/CodeGenerator.java index 747018b..a133634 100644 --- a/ym-generator/src/main/java/com/cnbm/generator/build/CodeGenerator.java +++ b/ym-generator/src/main/java/com/cnbm/generator/build/CodeGenerator.java @@ -9,11 +9,13 @@ import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine; import com.baomidou.mybatisplus.generator.function.ConverterFileName; import com.cnbm.generator.config.DataConfig; import com.cnbm.generator.engine.EnhanceVelocityTemplateEngine; +import com.cnbm.generator.utils.IdWorker; import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; import java.io.File; import java.util.*; +import java.util.function.BiConsumer; import static com.baomidou.mybatisplus.generator.config.ConstVal.MODULE_NAME; @@ -27,7 +29,7 @@ public class CodeGenerator { @Test public void test(){ - mybatisPlusGenerator(new String[]{"sys_user"}); + mybatisPlusGenerator(new String[]{"sys_user","sys_role"}); } public static void mybatisPlusGenerator(String[] include){ @@ -36,9 +38,9 @@ public class CodeGenerator { Map customFile = new HashMap<>(); customFile.put("DTO","/templates/DTO.java.vm"); customFile.put("Excel","/templates/Excel.java.vm"); + customFile.put("mysql","/templates/mysql.vm"); + -// customFile.put("Excel","/templates/excel.java.vm"); -// new TemplateConfig.Builder().serviceImpl("/templates/serviceImpl.java"); FastAutoGenerator.create(DataConfig.url,DataConfig.username,DataConfig.password) .globalConfig(builder -> { builder.author("why") @@ -61,87 +63,13 @@ public class CodeGenerator { //设置需要生成的表名 builder.addInclude(include) //设置过滤表前缀 - .addTablePrefix("sys_") -// .entityBuilder().formatFileName("%sEntity") -// .mapperBuilder().formatMapperFileName("%sMapper").formatXmlFileName("%sMapper") -// .controllerBuilder().formatFileName("%sController").enableRestStyle() -// .serviceBuilder().formatServiceFileName("%sService").formatServiceImplFileName("%sServiceImpl") - ; + .addTablePrefix("sys_"); }) .injectionConfig(consumer -> { consumer.customFile(customFile); }) .templateEngine(new EnhanceVelocityTemplateEngine()) -// .templateEngine(new VelocityTemplateEngine(){ -// @Override -// protected void outputCustomFile(@NotNull Map customFile, @NotNull TableInfo tableInfo, @NotNull Map objectMap) { -// //存放取出的实体名称,用于生成路由 -// List entityNames = new ArrayList<>(); -// -// if (!entityNames.contains(tableInfo.getEntityName())) { -// entityNames.add(tableInfo.getEntityName()); -// } -// -// customFile.forEach((key, value) -> { -// String fileName = String.format(path + "/src/main/resources/static/" + tableInfo.getEntityName() + File.separator + tableInfo.getEntityName() + "%s", key); -// this.outputFile(new File(fileName), objectMap, ""); -// }); -// -// // 生成路由部分 -// Map routers = new HashMap<>(); -// routers.put("author", "why"); -// routers.put("date", new Date()); -// routers.put("entities", entityNames); -// -// // 使用 freemarker 模板引擎,路由页面路径 -// String templateRoutesPath = "/templates/DTO.java.vm"; -// -// // 生成的路由页面路径 -// File templateRoutesOutFile = new File(path + "/src/main/java/com/cnbm/generator/engine/"+tableInfo.getEntityName() +"DTO.java"); -// try { -// this.writer(routers, templateRoutesPath, templateRoutesOutFile); -// } catch (Exception e) { -// throw new RuntimeException(e); -// } -// } -// }) - - -// .templateEngine(new FreemarkerTemplateEngine(){ -// @Override -// protected void outputCustomFile(@NotNull Map customFile, @NotNull TableInfo tableInfo, @NotNull Map objectMap) { -// //存放取出的实体名称,用于生成路由 -// List entityNames = new ArrayList<>(); -// -// if (!entityNames.contains(tableInfo.getEntityName())) { -// entityNames.add(tableInfo.getEntityName()); -// } -// -// customFile.forEach((key, value) -> { -// String fileName = String.format(path + "/src/main/resources/static/" + tableInfo.getEntityName() + File.separator + tableInfo.getEntityName() + "%s", key); -// this.outputFile(new File(fileName), objectMap, ""); -// }); -// -// // 生成路由部分 -// Map routers = new HashMap<>(); -// routers.put("author", "why"); -// routers.put("date", new Date()); -// routers.put("entities", entityNames); -// -// // 使用 freemarker 模板引擎,路由页面路径 -// String templateRoutesPath = "/templates/excel.java.ftl"; -// -// // 生成的路由页面路径 -// File templateRoutesOutFile = new File(path + "/src/main/java/com/cnbm/generator/engine/excel.java"); -// try { -// this.writer(routers, templateRoutesPath, templateRoutesOutFile); -// } catch (Exception e) { -// throw new RuntimeException(e); -// } -// } -// -// }) .execute(); } diff --git a/ym-generator/src/main/java/com/cnbm/generator/engine/EnhanceVelocityTemplateEngine.java b/ym-generator/src/main/java/com/cnbm/generator/engine/EnhanceVelocityTemplateEngine.java index 7d79c0f..bf0f12f 100644 --- a/ym-generator/src/main/java/com/cnbm/generator/engine/EnhanceVelocityTemplateEngine.java +++ b/ym-generator/src/main/java/com/cnbm/generator/engine/EnhanceVelocityTemplateEngine.java @@ -3,6 +3,7 @@ package com.cnbm.generator.engine; import com.baomidou.mybatisplus.generator.config.OutputFile; import com.baomidou.mybatisplus.generator.config.po.TableInfo; import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine; +import com.cnbm.generator.utils.IdWorker; import org.jetbrains.annotations.NotNull; import java.io.File; @@ -21,6 +22,9 @@ public class EnhanceVelocityTemplateEngine extends VelocityTemplateEngine { // String otherPath = this.getPathInfo(OutputFile.other); File file = new File(""); String path = file.getAbsolutePath(); + for (int i = 0; i < 6; i++) { + objectMap.put("id"+ i, IdWorker.getId()); + } customFile.forEach((key, value) -> { String fileName = ""; if ("DTO".equals(key)) { @@ -29,6 +33,9 @@ public class EnhanceVelocityTemplateEngine extends VelocityTemplateEngine { if ("Excel".equals(key)) { fileName = String.format((path+ File.separator +"src/main/java/com/cnbm/generator/code"+ File.separator + "excel" + File.separator + entityName + "%s" + ".java"), key); } + if ("mysql".equals(key)) { + fileName = String.format((path+ File.separator +"src/main/java/com/cnbm/generator/code"+ File.separator + "mysql" + File.separator + entityName + "%s" + ".sql"), key); + } this.outputFile(new File(fileName), objectMap, value); }); }