From 9c649ccdcda8696aaa30306b10c4c3a0a27c7036 Mon Sep 17 00:00:00 2001 From: LGH <1746689524@qq.com> Date: Tue, 8 Nov 2022 13:54:59 +0800 Subject: [PATCH] =?UTF-8?q?codeGenerator=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cnbm/generator/build/CodeGenerator.java | 70 +++++++++++++++++++ .../engine/EnhanceVelocityTemplateEngine.java | 62 ++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 ym-generator/src/main/java/com/cnbm/generator/build/CodeGenerator.java create mode 100644 ym-generator/src/main/java/com/cnbm/generator/engine/EnhanceVelocityTemplateEngine.java 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 new file mode 100644 index 0000000..3d09431 --- /dev/null +++ b/ym-generator/src/main/java/com/cnbm/generator/build/CodeGenerator.java @@ -0,0 +1,70 @@ +package com.cnbm.generator.build; + +import com.baomidou.mybatisplus.generator.FastAutoGenerator; +import com.cnbm.generator.config.DataConfig; +import com.cnbm.generator.engine.EnhanceVelocityTemplateEngine; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; + +/** + * @Author weihongyang + * @Date 2022/6/14 8:47 AM + * @Version 1.0 + */ +public class CodeGenerator { + + + @Test + public void test(){ + mybatisPlusGenerator(new String[]{"t_test"}); + } + + public static void mybatisPlusGenerator(String[] include){ + File file = new File(""); + String path = file.getAbsolutePath(); + Map customFile = new HashMap<>(); + customFile.put("DTO","/templates/DTO.java.vm"); + customFile.put("Excel","/templates/Excel.java.vm"); + customFile.put("mysql","/templates/mysql.vm"); + + + FastAutoGenerator.create(DataConfig.url,DataConfig.username,DataConfig.password) + .globalConfig(builder -> { + builder.author("codeGenerator") + .enableSwagger() + .fileOverride() + .disableOpenDir() + .outputDir(path+"/src/main/java"); + }) + .packageConfig(builder -> { + //设置父包名 + builder.parent("com.cnbm.generator") + .moduleName("code") + .service("service") + .serviceImpl("service.impl") + .entity("entity") + .mapper("mapper") + .controller("controller") + .xml("mapper"); + }) + .strategyConfig(builder -> { + //设置需要生成的表名 + builder.addInclude(include) + //设置过滤表前缀 + .addTablePrefix("sys_","t_") + .serviceBuilder().formatServiceImplFileName("%sServiceBizImpl").formatServiceFileName("%sServiceBiz") + //添加逻辑删除列 + .entityBuilder().logicDeleteColumnName("valid").enableColumnConstant().build(); + }) + .injectionConfig(consumer -> { + + consumer.customFile(customFile); + }) + .templateEngine(new EnhanceVelocityTemplateEngine()) + .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 new file mode 100644 index 0000000..bf0f12f --- /dev/null +++ b/ym-generator/src/main/java/com/cnbm/generator/engine/EnhanceVelocityTemplateEngine.java @@ -0,0 +1,62 @@ +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; +import java.util.Map; + +/** + * @Author weihongyang + * @Date 2022/6/17 10:27 AM + * @Version 1.0 + */ +public class EnhanceVelocityTemplateEngine extends VelocityTemplateEngine { + @Override + protected void outputCustomFile(@NotNull Map customFile, @NotNull TableInfo tableInfo, @NotNull Map objectMap) { + String entityName = tableInfo.getEntityName(); + System.out.println(entityName); +// 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)) { + fileName = String.format((path+ File.separator +"src/main/java/com/cnbm/generator/code"+ File.separator + "dto" + File.separator + entityName + "%s" + ".java"), key); + } + 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); + }); + } + +// @Override +// public void writer(Map objectMap, String templatePath, File outputFile) throws Exception { +// // 获取table +// TableInfo tableInfo = (TableInfo) objectMap.get("table"); +// // 取出Service接口的名字,进行首字母小写处理 +// String serviceNameFirstWord = tableInfo.getServiceName().substring(0,1).toLowerCase(); +// String serviceNameFirstWordToLower = serviceNameFirstWord + tableInfo.getServiceName().substring(1); +// objectMap.put("serviceNameFirstWordToLower", serviceNameFirstWordToLower); +// +// // 取出mapper接口的名字,进行首字母小写处理 +// String mapperNameFirstWord = tableInfo.getMapperName().substring(0, 1).toLowerCase(); +// String mapperNameFirstWordToLower = mapperNameFirstWord + tableInfo.getMapperName().substring(1); +// objectMap.put("mapperNameFirstWordToLower", mapperNameFirstWordToLower); +// +// // 对Form、VO文件的处理 +//// new FormHandler().handler(objectMap); +// super.writer(objectMap, templatePath, outputFile); +// } + +}