修改 request 包.
This commit is contained in:
@@ -14,14 +14,10 @@ import com.orion.ops.framework.mybatis.core.domain.BaseDO;
|
||||
import com.orion.ops.framework.mybatis.core.mapper.IMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.File;
|
||||
import java.sql.Types;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -34,18 +30,20 @@ import java.util.stream.Collectors;
|
||||
public class CodeGenerator {
|
||||
|
||||
public static void main(String[] args) {
|
||||
@NotNull
|
||||
// 输出路径
|
||||
String outputDir = "D:/MP/";
|
||||
@NotNull
|
||||
// 作者
|
||||
String author = Const.ORION_AUTHOR;
|
||||
@NotEmpty
|
||||
String[] tables = {"system_user", "system_role", "system_user_role", "system_menu", "system_role_menu"};
|
||||
// 表业务注释 需要和表一一对应 null则为表注释
|
||||
@NotEmpty
|
||||
String[] comment = {"用户", "角色", "用户角色关联", "菜单", "角色菜单关联"};
|
||||
// 模块
|
||||
@NotNull
|
||||
String module = "infra";
|
||||
// 生成的表 表 - 业务注释 - request文件的包前缀
|
||||
GenTable[] tables = {
|
||||
new GenTable("system_user", "用户", "user"),
|
||||
new GenTable("system_role", "角色", "role"),
|
||||
new GenTable("system_user_role", "用户角色关联", "role"),
|
||||
new GenTable("system_menu", "菜单", "menu"),
|
||||
new GenTable("system_role_menu", "角色菜单关联", "menu")
|
||||
};
|
||||
// jdbc 配置 - 使用配置文件
|
||||
File yamlFile = new File("orion-ops-launch/src/main/resources/application-dev.yaml");
|
||||
YmlExt yaml = YmlExt.load(yamlFile);
|
||||
@@ -56,7 +54,7 @@ public class CodeGenerator {
|
||||
// 执行
|
||||
runGenerator(outputDir, author,
|
||||
url, username, password,
|
||||
tables, comment, module);
|
||||
tables, module);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,11 +65,10 @@ public class CodeGenerator {
|
||||
String url,
|
||||
String username,
|
||||
String password,
|
||||
String[] tables,
|
||||
String[] comment,
|
||||
GenTable[] tables,
|
||||
String module) {
|
||||
// 创建引擎
|
||||
VelocityTemplateEngine engine = getEngine(tables, comment);
|
||||
VelocityTemplateEngine engine = getEngine(tables);
|
||||
|
||||
// 获取全局配置
|
||||
GlobalConfig globalConfig = getGlobalConfig(outputDir, author);
|
||||
@@ -111,20 +108,11 @@ public class CodeGenerator {
|
||||
/**
|
||||
* 获取渲染引擎
|
||||
*
|
||||
* @param tables 表
|
||||
* @param comment 表注释
|
||||
* @return
|
||||
* @param tables 表
|
||||
* @return 渲染引擎
|
||||
*/
|
||||
private static VelocityTemplateEngine getEngine(String[] tables, String[] comment) {
|
||||
if (tables.length != comment.length) {
|
||||
throw new IllegalArgumentException("表称与业务注释长度不匹配");
|
||||
}
|
||||
// 业务注释
|
||||
Map<String, String> tableComment = new HashMap<>();
|
||||
for (int i = 0; i < tables.length; i++) {
|
||||
tableComment.put(tables[i], comment[i]);
|
||||
}
|
||||
return new VelocityTemplateEngine(tableComment);
|
||||
private static VelocityTemplateEngine getEngine(GenTable[] tables) {
|
||||
return new VelocityTemplateEngine(tables);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -185,10 +173,13 @@ public class CodeGenerator {
|
||||
* @param tables 生成的表名
|
||||
* @return 策略配置
|
||||
*/
|
||||
private static StrategyConfig getStrategyConfig(String[] tables) {
|
||||
private static StrategyConfig getStrategyConfig(GenTable[] tables) {
|
||||
String[] tableNames = Arrays.stream(tables)
|
||||
.map(GenTable::getTableName)
|
||||
.toArray(String[]::new);
|
||||
StrategyConfig stConfig = new StrategyConfig.Builder()
|
||||
// 生成的表
|
||||
.addInclude(tables)
|
||||
.addInclude(tableNames)
|
||||
// 全局大写命名
|
||||
.enableCapitalMode()
|
||||
// 实体配置
|
||||
@@ -308,11 +299,11 @@ public class CodeGenerator {
|
||||
// dto 文件
|
||||
new String[]{"/templates/orion-entity-dto.java.vm", "%sDTO.java", "entity.dto"},
|
||||
// create request 文件
|
||||
new String[]{"/templates/orion-entity-request-create.java.vm", "%sCreateRequest.java", "entity.request"},
|
||||
new String[]{"/templates/orion-entity-request-create.java.vm", "%sCreateRequest.java", "entity.request.%s"},
|
||||
// update request 文件
|
||||
new String[]{"/templates/orion-entity-request-update.java.vm", "%sUpdateRequest.java", "entity.request"},
|
||||
new String[]{"/templates/orion-entity-request-update.java.vm", "%sUpdateRequest.java", "entity.request.%s"},
|
||||
// query request 文件
|
||||
new String[]{"/templates/orion-entity-request-query.java.vm", "%sQueryRequest.java", "entity.request"},
|
||||
new String[]{"/templates/orion-entity-request-query.java.vm", "%sQueryRequest.java", "entity.request.%s"},
|
||||
// convert 文件
|
||||
new String[]{"/templates/orion-convert.java.vm", "%sConvert.java", "convert"},
|
||||
// convert provider 文件
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.orion.ops.framework.mybatis.core.generator;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023/7/17 10:44
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class GenTable {
|
||||
|
||||
/**
|
||||
* 表名称
|
||||
*/
|
||||
private String tableName;
|
||||
|
||||
/**
|
||||
* 注释
|
||||
*/
|
||||
private String comment;
|
||||
|
||||
/**
|
||||
* 请求实体包名
|
||||
*/
|
||||
private String requestPackage;
|
||||
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
|
||||
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
|
||||
import com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine;
|
||||
import com.orion.lang.utils.Strings;
|
||||
import com.orion.lang.utils.reflect.Fields;
|
||||
import com.orion.ops.framework.common.constant.Const;
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
@@ -35,10 +36,9 @@ import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -50,12 +50,15 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
public class VelocityTemplateEngine extends AbstractTemplateEngine {
|
||||
|
||||
private final Map<String, String> tableComment;
|
||||
private static final String REQUEST_PACKAGE_REPLACER = "request.%s";
|
||||
|
||||
private final Map<String, GenTable> tables;
|
||||
|
||||
private VelocityEngine velocityEngine;
|
||||
|
||||
public VelocityTemplateEngine(Map<String, String> tableComment) {
|
||||
this.tableComment = tableComment;
|
||||
public VelocityTemplateEngine(GenTable[] tables) {
|
||||
this.tables = Arrays.stream(tables)
|
||||
.collect(Collectors.toMap(GenTable::getTableName, Function.identity()));
|
||||
}
|
||||
|
||||
{
|
||||
@@ -99,13 +102,84 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
|
||||
return filePath.endsWith(dotVm) ? filePath : filePath + dotVm;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建自定义文件副本对象
|
||||
*
|
||||
* @param originCustomerFile originCustomerFile
|
||||
* @return backup
|
||||
*/
|
||||
private List<CustomFile> createCustomFilesBackup(@NotNull List<CustomFile> originCustomerFile) {
|
||||
// 生成文件副本
|
||||
List<CustomFile> customFiles = originCustomerFile.stream().map(s -> {
|
||||
return new CustomFile.Builder()
|
||||
.enableFileOverride()
|
||||
.templatePath(s.getTemplatePath())
|
||||
.filePath(s.getFilePath())
|
||||
.fileName(s.getFileName())
|
||||
.packageName(s.getPackageName())
|
||||
.build();
|
||||
}).collect(Collectors.toList());
|
||||
return customFiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换自定义文件包
|
||||
*
|
||||
* @param customFiles 自定义文件
|
||||
* @param tableInfo tableInfo
|
||||
* @param objectMap objectMap
|
||||
*/
|
||||
private void replacePackageName(@NotNull List<CustomFile> customFiles, @NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
|
||||
// 替换包名
|
||||
customFiles.forEach(s -> {
|
||||
// 反射调用 setter 方法
|
||||
BiConsumer<String, Object> callSetter = (field, value) -> Fields.setFieldValue(s, field, value);
|
||||
String packageName = s.getPackageName();
|
||||
// 替换 Request.java 文件包名
|
||||
if (packageName.contains(REQUEST_PACKAGE_REPLACER)) {
|
||||
String replacePackage = String.format(packageName, tables.get(tableInfo.getName()).getRequestPackage());
|
||||
callSetter.accept("packageName", replacePackage);
|
||||
}
|
||||
});
|
||||
// 自定义文件的包 (导入用)
|
||||
List<String> customFilePackages = customFiles.stream()
|
||||
.filter(s -> s.getTemplatePath().contains(".java.vm"))
|
||||
.map(CustomFile::getPackageName)
|
||||
.map(s -> getConfigBuilder().getPackageConfig().getParent() + "." + s)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
objectMap.put("customFilePackages", customFilePackages);
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入表元数据
|
||||
*
|
||||
* @param tableInfo tableInfo
|
||||
* @param objectMap objectMap
|
||||
*/
|
||||
private void addTableMeta(@NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
|
||||
// http 注释标识
|
||||
objectMap.put("httpComment", "###");
|
||||
// 版本
|
||||
objectMap.put("since", Const.VERSION);
|
||||
// 替换业务注释
|
||||
tableInfo.setComment(tables.get(tableInfo.getName()).getComment());
|
||||
// 实体名称
|
||||
String domainName = tableInfo.getEntityName();
|
||||
String mappingHyphen = objectMap.get("controllerMappingHyphen").toString();
|
||||
String entityName = domainName.substring(0, domainName.length() - 2);
|
||||
objectMap.put("type", entityName);
|
||||
objectMap.put("typeLower", Strings.firstLower(entityName));
|
||||
objectMap.put("typeHyphen", mappingHyphen.substring(0, mappingHyphen.length() - 3));
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入 api 注释
|
||||
*
|
||||
* @param tableInfo tableInfo
|
||||
* @param objectMap objectMap
|
||||
*/
|
||||
private void putApiComment(TableInfo tableInfo, Map<String, Object> objectMap) {
|
||||
private void addApiCommentMeta(@NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
objectMap.put("apiComment", map);
|
||||
String comment = tableInfo.getComment();
|
||||
@@ -118,46 +192,16 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
|
||||
map.put("batchDelete", "通过 id 批量删除" + comment);
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出自定义模板文件
|
||||
*
|
||||
* @param customFiles 自定义模板文件列表
|
||||
* @param tableInfo 表信息
|
||||
* @param objectMap 渲染数据
|
||||
* @since 3.5.3
|
||||
*/
|
||||
@Override
|
||||
protected void outputCustomFile(@NotNull List<CustomFile> customFiles, @NotNull TableInfo tableInfo, @NotNull Map<String, Object> objectMap) {
|
||||
// 替换业务注释
|
||||
String comment = tableComment.get(tableInfo.getName());
|
||||
if (comment != null) {
|
||||
tableInfo.setComment(comment);
|
||||
}
|
||||
|
||||
// http 注释标识
|
||||
objectMap.put("httpComment", "###");
|
||||
// 版本
|
||||
objectMap.put("since", Const.VERSION);
|
||||
|
||||
// 实体名称
|
||||
String domainName = tableInfo.getEntityName();
|
||||
String mappingHyphen = objectMap.get("controllerMappingHyphen").toString();
|
||||
String entityName = domainName.substring(0, domainName.length() - 2);
|
||||
objectMap.put("type", entityName);
|
||||
objectMap.put("typeLower", Strings.firstLower(entityName));
|
||||
objectMap.put("typeHyphen", mappingHyphen.substring(0, mappingHyphen.length() - 3));
|
||||
// 注释
|
||||
this.putApiComment(tableInfo, objectMap);
|
||||
|
||||
// 自定义文件的包
|
||||
List<String> customFilePackages = customFiles.stream()
|
||||
.filter(s -> s.getTemplatePath().contains(".java.vm"))
|
||||
.map(CustomFile::getPackageName)
|
||||
.map(s -> getConfigBuilder().getPackageConfig().getParent() + "." + s)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
// 设置导入的包
|
||||
objectMap.put("customFilePackages", customFilePackages);
|
||||
// 创建自定义文件副本文件
|
||||
customFiles = this.createCustomFilesBackup(customFiles);
|
||||
// 替换自定义包名
|
||||
this.replacePackageName(customFiles, tableInfo, objectMap);
|
||||
// 添加表元数据
|
||||
this.addTableMeta(tableInfo, objectMap);
|
||||
// 添加注释元数据
|
||||
this.addApiCommentMeta(tableInfo, objectMap);
|
||||
|
||||
// 生成文件
|
||||
String parentPath = getPathInfo(OutputFile.parent);
|
||||
@@ -173,7 +217,7 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
|
||||
filePath = filePath + File.separator + file.getPackageName();
|
||||
filePath = filePath.replaceAll("\\.", StringPool.BACK_SLASH + File.separator);
|
||||
}
|
||||
String fileName = filePath + File.separator + String.format(file.getFileName(), entityName);
|
||||
String fileName = filePath + File.separator + String.format(file.getFileName(), objectMap.get("type"));
|
||||
outputFile(new File(fileName), objectMap, file.getTemplatePath(), file.isFileOverride());
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user