fix: 修复生成bug和缺陷

This commit is contained in:
bunny 2025-04-15 19:11:38 +08:00
parent e2b6d49518
commit 474f3ab89b
4 changed files with 41 additions and 39 deletions

View File

@ -107,7 +107,5 @@
<artifactId>velocity-engine-core</artifactId> <artifactId>velocity-engine-core</artifactId>
<version>2.3</version> <version>2.3</version>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -52,26 +52,26 @@ public class VmsServiceImpl implements VmsService {
// 表格属性名 列信息 // 表格属性名 列信息
TableInfoVo tableMetaData = tableService.getTableMetaData(tableName); TableInfoVo tableMetaData = tableService.getTableMetaData(tableName);
List<ColumnMetaData> columnInfoList = tableService.getColumnInfo(tableName); List<ColumnMetaData> columnInfoList = tableService.getColumnInfo(tableName).stream().distinct().toList();
List<String> list = columnInfoList.stream().map(ColumnMetaData::getColumnName).toList(); List<String> list = columnInfoList.stream().map(ColumnMetaData::getColumnName).toList();
// 添加要生成的属性 // 添加要生成的属性
VelocityContext context = new VelocityContext(); VelocityContext context = new VelocityContext();
// 当前的表名 // 当前的表名
context.put("tableName", tableMetaData.getTableName()); context.put("tableName" , tableMetaData.getTableName());
// 表字段的注释内容 // 表字段的注释内容
context.put("comment", tableMetaData.getComment()); context.put("comment" , tableMetaData.getComment());
// 设置包名称 // 设置包名称
context.put("package", dto.getPackageName()); context.put("package" , dto.getPackageName());
// 当前表的列信息 // 当前表的列信息
context.put("columnInfoList", columnInfoList); context.put("columnInfoList" , columnInfoList);
// 数据库sql列 // 数据库sql列
context.put("baseColumnList", String.join(",", list)); context.put("baseColumnList" , String.join("," , list));
// 生成模板 // 生成模板
VmsUtil.commonVms(writer, context, "vms/" + path, dto); VmsUtil.commonVms(writer, context, "vms/" + path, dto);
@ -97,11 +97,11 @@ public class VmsServiceImpl implements VmsService {
@Override @Override
public Map<String, List<VmsPathVo>> getVmsPathList() { public Map<String, List<VmsPathVo>> getVmsPathList() {
// 读取当前项目中所有的 vm 模板发给前端 // 读取当前项目中所有的 vm 模板发给前端
List<String> vmsRelativeFiles = ResourceFileUtil.getRelativeFiles("vms"); List<String> vmsRelativeFiles = ResourceFileUtil.getRelativeFiles("vms" );
return vmsRelativeFiles.stream().map(vmFile -> { return vmsRelativeFiles.stream().map(vmFile -> {
String[] filepathList = vmFile.split("/"); String[] filepathList = vmFile.split("/" );
String filename = filepathList[filepathList.length - 1].replace(".vm", ""); String filename = filepathList[filepathList.length - 1].replace(".vm" , "" );
return VmsPathVo.builder().name(vmFile).label(filename).type(filepathList[0]).build(); return VmsPathVo.builder().name(vmFile).label(filename).type(filepathList[0]).build();
}) })
@ -125,7 +125,7 @@ public class VmsServiceImpl implements VmsService {
// 2. 遍历并创建 // 2. 遍历并创建
generatorVoList.forEach(generatorVo -> { generatorVoList.forEach(generatorVo -> {
// zip中的路径 // zip中的路径
String path = generatorVo.getPath().replace(".vm", ""); String path = generatorVo.getPath().replace(".vm" , "" );
// zip中的文件 // zip中的文件
String code = generatorVo.getCode(); String code = generatorVo.getCode();
@ -148,14 +148,14 @@ public class VmsServiceImpl implements VmsService {
// 2.1 文件不重名 // 2.1 文件不重名
long currentTimeMillis = System.currentTimeMillis(); long currentTimeMillis = System.currentTimeMillis();
String digestHex = MD5.create().digestHex(currentTimeMillis + ""); String digestHex = MD5.create().digestHex(currentTimeMillis + "" );
// 3. 准备响应 // 3. 准备响应
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment; filename=" + "vms-" + digestHex + ".zip"); headers.add("Content-Disposition" , "attachment; filename=" + "vms-" + digestHex + ".zip" );
headers.add("Cache-Control", "no-cache, no-store, must-revalidate"); headers.add("Cache-Control" , "no-cache, no-store, must-revalidate" );
headers.add("Pragma", "no-cache"); headers.add("Pragma" , "no-cache" );
headers.add("Expires", "0"); headers.add("Expires" , "0" );
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
return new ResponseEntity<>(byteArrayInputStream.readAllBytes(), headers, HttpStatus.OK); return new ResponseEntity<>(byteArrayInputStream.readAllBytes(), headers, HttpStatus.OK);

View File

@ -15,11 +15,11 @@ import java.util.concurrent.atomic.AtomicReference;
public class VmsUtil { public class VmsUtil {
private static final Map<String, String> TYPE_MAPPINGS = Map.of( private static final Map<String, String> TYPE_MAPPINGS = Map.of(
"controller", "Controller", "controller" , "Controller" ,
"service", "Service", "service" , "Service" ,
"serviceImpl", "ServiceImpl", "serviceImpl" , "ServiceImpl" ,
"mapper", "Mapper", "mapper" , "Mapper" ,
"resourceMapper", "Mapper" "resourceMapper" , "Mapper"
); );
/** /**
@ -45,33 +45,33 @@ public class VmsUtil {
// 表前缀 转成数组 // 表前缀 转成数组
AtomicReference<String> replaceTableName = new AtomicReference<>(className); AtomicReference<String> replaceTableName = new AtomicReference<>(className);
for (String prefix : tablePrefixes.split("[,]")) { for (String prefix : tablePrefixes.split("[,]" )) {
replaceTableName.set(className.replace(prefix, "")); replaceTableName.set(className.replace(prefix, "" ));
} }
String date = new SimpleDateFormat(dto.getSimpleDateFormat()).format(new Date()); String date = new SimpleDateFormat(dto.getSimpleDateFormat()).format(new Date());
// vm 不能直接写 `{` 需要转换下 // vm 不能直接写 `{` 需要转换下
context.put("leftBrace", "{"); context.put("leftBrace" , "{" );
// 当前日期 // 当前日期
context.put("date", date); context.put("date" , date);
// 作者名字 // 作者名字
context.put("author", author); context.put("author" , author);
// 每个 Controller 上的请求前缀 // 每个 Controller 上的请求前缀
context.put("requestMapping", requestMapping); context.put("requestMapping" , requestMapping);
// 将类名称转成小驼峰 // 将类名称转成小驼峰
String toCamelCase = ConvertUtil.convertToCamelCase(replaceTableName.get()); String toCamelCase = ConvertUtil.convertToCamelCase(replaceTableName.get());
context.put("classLowercaseName", toCamelCase); context.put("classLowercaseName" , toCamelCase);
// 将类名称转成大驼峰 // 将类名称转成大驼峰
String convertToCamelCase = ConvertUtil.convertToCamelCase(replaceTableName.get(), true); String convertToCamelCase = ConvertUtil.convertToCamelCase(replaceTableName.get(), true);
context.put("classUppercaseName", convertToCamelCase); context.put("classUppercaseName" , convertToCamelCase);
// Velocity 生成模板 // Velocity 生成模板
Template servicePathTemplate = Velocity.getTemplate(templateName, "UTF-8"); Template servicePathTemplate = Velocity.getTemplate(templateName, "UTF-8" );
servicePathTemplate.merge(context, writer); servicePathTemplate.merge(context, writer);
} }
@ -82,7 +82,7 @@ public class VmsUtil {
* @param className 类名 * @param className 类名
*/ */
public static String handleVmFilename(String path, String className) { public static String handleVmFilename(String path, String className) {
String[] splitPaths = path.split("/"); String[] splitPaths = path.split("/" );
int splitPathsSize = splitPaths.length - 1; int splitPathsSize = splitPaths.length - 1;
// 大驼峰名称 // 大驼峰名称
@ -92,23 +92,27 @@ public class VmsUtil {
// 当前文件名 // 当前文件名
String filename = splitPaths[splitPathsSize]; String filename = splitPaths[splitPathsSize];
filename = filename.replace(".vm", ""); filename = filename.replace(".vm" , "" );
String[] split = filename.split("\\." );
// 文件名称 // 文件名称
String name = filename.split("\\.")[0]; String name = split[0];
// 文件扩展名 // 文件扩展名
String extension = filename.split("\\.")[1]; String extension = "";
if (split.length >= 2) {
extension = split[1];
}
// 判断是否是 Java 或者 xml 文件 // 判断是否是 Java 或者 xml 文件
if (filename.contains("java") || filename.contains("xml")) { if (filename.contains("java" ) || filename.contains("xml" )) {
filename = CamelCase + TYPE_MAPPINGS.get(name) + "." + extension; filename = CamelCase + TYPE_MAPPINGS.get(name) + "." + extension;
} }
if (filename.contains("vue") && !filename.contains("index")) { if (filename.contains("vue" ) && !filename.contains("index" )) {
filename = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, camelCase) + "-" + name + "." + extension; filename = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, camelCase) + "-" + name + "." + extension;
} }
splitPaths[splitPathsSize] = filename; splitPaths[splitPathsSize] = filename;
return String.join("/", splitPaths); return String.join("/" , splitPaths);
} }
} }

View File

@ -5,7 +5,7 @@
<!-- 通用查询映射结果 --> <!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="${classUppercaseName}"> <resultMap id="BaseResultMap" type="${classUppercaseName}">
#foreach($field in ${columnInfoList}) #foreach($field in ${columnInfoList})
<id column="${field.column}" property="${field.fieldName}"/> <id column="${field.columnName}" property="${field.fieldName}"/>
#end #end
</resultMap> </resultMap>