commit init

This commit is contained in:
weihongyang
2022-06-20 16:26:51 +08:00
commit 7aaa6700b3
171 changed files with 9178 additions and 0 deletions

View File

@@ -0,0 +1,148 @@
package com.cnbm.generator.build;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
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 org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.*;
import static com.baomidou.mybatisplus.generator.config.ConstVal.MODULE_NAME;
/**
* @Author weihongyang
* @Date 2022/6/14 8:47 AM
* @Version 1.0
*/
public class CodeGenerator {
@Test
public void test(){
mybatisPlusGenerator(new String[]{"sys_user"});
}
public static void mybatisPlusGenerator(String[] include){
File file = new File("");
String path = file.getAbsolutePath();
Map<String, String> customFile = new HashMap<>();
customFile.put("DTO","/templates/DTO.java.vm");
customFile.put("Excel","/templates/Excel.java.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")
.enableSwagger()
.fileOverride()
.outputDir(path+"/src/main/java");
})
.packageConfig(builder -> {
//设置父包名
builder.parent("com.cnbm")
.moduleName("generator")
.service("service")
.serviceImpl("service.impl")
.entity("entity")
.mapper("mapper")
.controller("controller")
.xml("mapper");
})
.strategyConfig(builder -> {
//设置需要生成的表名
builder.addInclude(include)
//设置过滤表前缀
.addTablePrefix("sys_")
// .entityBuilder().formatFileName("%sEntity")
// .mapperBuilder().formatMapperFileName("%sMapper").formatXmlFileName("%sMapper")
// .controllerBuilder().formatFileName("%sController").enableRestStyle()
// .serviceBuilder().formatServiceFileName("%sService").formatServiceImplFileName("%sServiceImpl")
;
})
.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();
}
}

View File

@@ -0,0 +1,16 @@
package com.cnbm.generator.config;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* @Author weihongyang
* @Date 2022/6/14 1:28 PM
* @Version 1.0
*/
public class DataConfig {
public static final String url = "jdbc:mysql://mysql.picaiba.com:30307/ym_pass?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true";
public static final String username = "root";
public static final String password = "1qaz@WSX3edc$RFV";
}

View File

@@ -0,0 +1,55 @@
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 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<String, String> customFile, @NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
String entityName = tableInfo.getEntityName();
System.out.println(entityName);
// String otherPath = this.getPathInfo(OutputFile.other);
File file = new File("");
String path = file.getAbsolutePath();
customFile.forEach((key, value) -> {
String fileName = "";
if ("DTO".equals(key)) {
fileName = String.format((path+ File.separator +"src/main/java/com/cnbm/generator"+ 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"+ File.separator + "excel" + File.separator + entityName + "%s" + ".java"), key);
}
this.outputFile(new File(fileName), objectMap, value);
});
}
// @Override
// public void writer(Map<String, Object> 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);
// }
}