feat: 新增一个swagger自动生成代码的模板引擎

This commit is contained in:
weihongyang 2022-06-22 14:02:11 +08:00
джерело e01cd0f7fd
коміт 89bf5bf10a
2 змінених файлів з 13 додано та 78 видалено

@ -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<String, String> 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<String, String> customFile, @NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
// //存放取出的实体名称用于生成路由
// List<String> 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<String, Object> 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<String, String> customFile, @NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
// //存放取出的实体名称用于生成路由
// List<String> 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<String, Object> 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();
}

@ -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);
});
}