fix: 修复生成bug和缺陷
This commit is contained in:
parent
e2b6d49518
commit
474f3ab89b
|
@ -107,7 +107,5 @@
|
|||
<artifactId>velocity-engine-core</artifactId>
|
||||
<version>2.3</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -52,26 +52,26 @@ public class VmsServiceImpl implements VmsService {
|
|||
|
||||
// 表格属性名 和 列信息
|
||||
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();
|
||||
|
||||
// 添加要生成的属性
|
||||
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列
|
||||
context.put("baseColumnList", String.join(",", list));
|
||||
context.put("baseColumnList" , String.join("," , list));
|
||||
|
||||
// 生成模板
|
||||
VmsUtil.commonVms(writer, context, "vms/" + path, dto);
|
||||
|
@ -97,11 +97,11 @@ public class VmsServiceImpl implements VmsService {
|
|||
@Override
|
||||
public Map<String, List<VmsPathVo>> getVmsPathList() {
|
||||
// 读取当前项目中所有的 vm 模板发给前端
|
||||
List<String> vmsRelativeFiles = ResourceFileUtil.getRelativeFiles("vms");
|
||||
List<String> vmsRelativeFiles = ResourceFileUtil.getRelativeFiles("vms" );
|
||||
|
||||
return vmsRelativeFiles.stream().map(vmFile -> {
|
||||
String[] filepathList = vmFile.split("/");
|
||||
String filename = filepathList[filepathList.length - 1].replace(".vm", "");
|
||||
String[] filepathList = vmFile.split("/" );
|
||||
String filename = filepathList[filepathList.length - 1].replace(".vm" , "" );
|
||||
|
||||
return VmsPathVo.builder().name(vmFile).label(filename).type(filepathList[0]).build();
|
||||
})
|
||||
|
@ -125,7 +125,7 @@ public class VmsServiceImpl implements VmsService {
|
|||
// 2. 遍历并创建
|
||||
generatorVoList.forEach(generatorVo -> {
|
||||
// zip中的路径
|
||||
String path = generatorVo.getPath().replace(".vm", "");
|
||||
String path = generatorVo.getPath().replace(".vm" , "" );
|
||||
|
||||
// zip中的文件
|
||||
String code = generatorVo.getCode();
|
||||
|
@ -148,14 +148,14 @@ public class VmsServiceImpl implements VmsService {
|
|||
|
||||
// 2.1 文件不重名
|
||||
long currentTimeMillis = System.currentTimeMillis();
|
||||
String digestHex = MD5.create().digestHex(currentTimeMillis + "");
|
||||
String digestHex = MD5.create().digestHex(currentTimeMillis + "" );
|
||||
|
||||
// 3. 准备响应
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add("Content-Disposition", "attachment; filename=" + "vms-" + digestHex + ".zip");
|
||||
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
headers.add("Pragma", "no-cache");
|
||||
headers.add("Expires", "0");
|
||||
headers.add("Content-Disposition" , "attachment; filename=" + "vms-" + digestHex + ".zip" );
|
||||
headers.add("Cache-Control" , "no-cache, no-store, must-revalidate" );
|
||||
headers.add("Pragma" , "no-cache" );
|
||||
headers.add("Expires" , "0" );
|
||||
|
||||
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
|
||||
return new ResponseEntity<>(byteArrayInputStream.readAllBytes(), headers, HttpStatus.OK);
|
||||
|
|
|
@ -15,11 +15,11 @@ import java.util.concurrent.atomic.AtomicReference;
|
|||
public class VmsUtil {
|
||||
|
||||
private static final Map<String, String> TYPE_MAPPINGS = Map.of(
|
||||
"controller", "Controller",
|
||||
"service", "Service",
|
||||
"serviceImpl", "ServiceImpl",
|
||||
"mapper", "Mapper",
|
||||
"resourceMapper", "Mapper"
|
||||
"controller" , "Controller" ,
|
||||
"service" , "Service" ,
|
||||
"serviceImpl" , "ServiceImpl" ,
|
||||
"mapper" , "Mapper" ,
|
||||
"resourceMapper" , "Mapper"
|
||||
);
|
||||
|
||||
/**
|
||||
|
@ -45,33 +45,33 @@ public class VmsUtil {
|
|||
|
||||
// 将 表前缀 转成数组
|
||||
AtomicReference<String> replaceTableName = new AtomicReference<>(className);
|
||||
for (String prefix : tablePrefixes.split("[,,]")) {
|
||||
replaceTableName.set(className.replace(prefix, ""));
|
||||
for (String prefix : tablePrefixes.split("[,,]" )) {
|
||||
replaceTableName.set(className.replace(prefix, "" ));
|
||||
}
|
||||
|
||||
String date = new SimpleDateFormat(dto.getSimpleDateFormat()).format(new Date());
|
||||
// vm 不能直接写 `{` 需要转换下
|
||||
context.put("leftBrace", "{");
|
||||
context.put("leftBrace" , "{" );
|
||||
|
||||
// 当前日期
|
||||
context.put("date", date);
|
||||
context.put("date" , date);
|
||||
|
||||
// 作者名字
|
||||
context.put("author", author);
|
||||
context.put("author" , author);
|
||||
|
||||
// 每个 Controller 上的请求前缀
|
||||
context.put("requestMapping", requestMapping);
|
||||
context.put("requestMapping" , requestMapping);
|
||||
|
||||
// 将类名称转成小驼峰
|
||||
String toCamelCase = ConvertUtil.convertToCamelCase(replaceTableName.get());
|
||||
context.put("classLowercaseName", toCamelCase);
|
||||
context.put("classLowercaseName" , toCamelCase);
|
||||
|
||||
// 将类名称转成大驼峰
|
||||
String convertToCamelCase = ConvertUtil.convertToCamelCase(replaceTableName.get(), true);
|
||||
context.put("classUppercaseName", convertToCamelCase);
|
||||
context.put("classUppercaseName" , convertToCamelCase);
|
||||
|
||||
// Velocity 生成模板
|
||||
Template servicePathTemplate = Velocity.getTemplate(templateName, "UTF-8");
|
||||
Template servicePathTemplate = Velocity.getTemplate(templateName, "UTF-8" );
|
||||
servicePathTemplate.merge(context, writer);
|
||||
}
|
||||
|
||||
|
@ -82,7 +82,7 @@ public class VmsUtil {
|
|||
* @param className 类名
|
||||
*/
|
||||
public static String handleVmFilename(String path, String className) {
|
||||
String[] splitPaths = path.split("/");
|
||||
String[] splitPaths = path.split("/" );
|
||||
int splitPathsSize = splitPaths.length - 1;
|
||||
|
||||
// 大驼峰名称
|
||||
|
@ -92,23 +92,27 @@ public class VmsUtil {
|
|||
|
||||
// 当前文件名
|
||||
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 文件
|
||||
if (filename.contains("java") || filename.contains("xml")) {
|
||||
if (filename.contains("java" ) || filename.contains("xml" )) {
|
||||
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;
|
||||
}
|
||||
|
||||
splitPaths[splitPathsSize] = filename;
|
||||
return String.join("/", splitPaths);
|
||||
return String.join("/" , splitPaths);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
<!-- 通用查询映射结果 -->
|
||||
<resultMap id="BaseResultMap" type="${classUppercaseName}">
|
||||
#foreach($field in ${columnInfoList})
|
||||
<id column="${field.column}" property="${field.fieldName}"/>
|
||||
<id column="${field.columnName}" property="${field.fieldName}"/>
|
||||
#end
|
||||
</resultMap>
|
||||
|
||||
|
|
Loading…
Reference in New Issue