feat: 生成文件名优化;大小写驼峰名转换优化
This commit is contained in:
parent
be1b09dc70
commit
5c241728f0
|
@ -95,11 +95,19 @@
|
|||
<artifactId>fastjson2</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 小驼峰 和 大驼峰之间互转 -->
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>33.4.7-jre</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.velocity</groupId>
|
||||
<artifactId>velocity-engine-core</artifactId>
|
||||
<version>2.3</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -76,6 +76,9 @@ public class VmsServiceImpl implements VmsService {
|
|||
// 生成模板
|
||||
VmsUtil.commonVms(writer, context, "vms/" + path, dto);
|
||||
|
||||
// 处理 vm 文件名
|
||||
path = VmsUtil.handleVmFilename(path, dto.getClassName());
|
||||
|
||||
return GeneratorVo.builder()
|
||||
.code(writer.toString())
|
||||
.comment(tableMetaData.getComment())
|
||||
|
|
|
@ -1,6 +1,12 @@
|
|||
package cn.bunny.utils;
|
||||
|
||||
import com.google.common.base.CaseFormat;
|
||||
import org.assertj.core.util.introspection.CaseFormatUtils;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ConvertUtil {
|
||||
|
||||
/**
|
||||
* 将数据库类型转换为Java类型
|
||||
*/
|
||||
|
@ -9,17 +15,17 @@ public class ConvertUtil {
|
|||
|
||||
columnType = columnType.toLowerCase();
|
||||
return switch (columnType) {
|
||||
case "varchar" , "char" , "text" , "longtext" , "mediumtext" , "tinytext" -> "String";
|
||||
case "int" , "integer" , "tinyint" , "smallint" -> "Integer";
|
||||
case "varchar", "char", "text", "longtext", "mediumtext", "tinytext" -> "String";
|
||||
case "int", "integer", "tinyint", "smallint" -> "Integer";
|
||||
case "bigint" -> "Long";
|
||||
case "decimal" , "numeric" -> "BigDecimal";
|
||||
case "decimal", "numeric" -> "BigDecimal";
|
||||
case "float" -> "Float";
|
||||
case "double" -> "Double";
|
||||
case "boolean" , "bit" , "tinyint unsigned" -> "Boolean";
|
||||
case "date" , "year" -> "Date";
|
||||
case "boolean", "bit", "tinyint unsigned" -> "Boolean";
|
||||
case "date", "year" -> "Date";
|
||||
case "time" -> "Time";
|
||||
case "datetime" , "timestamp" -> "LocalDateTime";
|
||||
case "blob" , "longblob" , "mediumblob" , "tinyblob" -> "byte[]";
|
||||
case "datetime", "timestamp" -> "LocalDateTime";
|
||||
case "blob", "longblob", "mediumblob", "tinyblob" -> "byte[]";
|
||||
default -> "Object";
|
||||
};
|
||||
}
|
||||
|
@ -34,45 +40,35 @@ public class ConvertUtil {
|
|||
/**
|
||||
* 下划线命名转驼峰命名
|
||||
*
|
||||
* @param name 原始名称
|
||||
* @param name 原始名称,传入的值可以是
|
||||
* `xxx_xxx` `CaseFormat`
|
||||
* `caseFormat`
|
||||
* @param firstLetterCapital 首字母是否大写
|
||||
*/
|
||||
public static String convertToCamelCase(String name, boolean firstLetterCapital) {
|
||||
if (name == null || name.isEmpty()) return name;
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
String[] parts = name.split("_" );
|
||||
// 首字母不大写
|
||||
if (!firstLetterCapital) return CaseFormatUtils.toCamelCase(name);
|
||||
|
||||
for (int i = 0; i < parts.length; i++) {
|
||||
String part = parts[i];
|
||||
if (part.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (i == 0 && !firstLetterCapital) {
|
||||
result.append(part.toLowerCase());
|
||||
} else {
|
||||
result.append(Character.toUpperCase(part.charAt(0)))
|
||||
.append(part.substring(1).toLowerCase());
|
||||
}
|
||||
// 检查是否全大写带下划线 (UPPER_UNDERSCORE)
|
||||
if (Pattern.matches("^[A-Z]+(_[A-Z]+)*$", name)) {
|
||||
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name);
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 辅助方法:将列名转换为字段名(如user_name -> userName)
|
||||
*
|
||||
* @param columnName 列名称
|
||||
* @return 列名称
|
||||
*/
|
||||
public static String convertToFieldName(String columnName) {
|
||||
String[] parts = columnName.split("_" );
|
||||
StringBuilder fieldName = new StringBuilder(parts[0].toLowerCase());
|
||||
for (int i = 1; i < parts.length; i++) {
|
||||
fieldName.append(parts[i].substring(0, 1).toUpperCase())
|
||||
.append(parts[i].substring(1).toLowerCase());
|
||||
// 检查是否小写带下划线 (LOWER_UNDERSCORE)
|
||||
if (Pattern.matches("^[a-z]+(_[a-z]+)*$", name)) {
|
||||
return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name);
|
||||
}
|
||||
return fieldName.toString();
|
||||
|
||||
// 检查是否大驼峰 (UpperCamelCase)
|
||||
if (Character.isUpperCase(name.charAt(0)) &&
|
||||
!name.contains("_") &&
|
||||
name.chars().anyMatch(Character::isLowerCase)) {
|
||||
return CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_CAMEL, name);
|
||||
}
|
||||
|
||||
// 默认认为是小驼峰 (lowerCamelCase)
|
||||
return CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, name);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -148,7 +148,7 @@ public class DbInfoUtil {
|
|||
// 设置列字段
|
||||
column.setColumnName(columnName);
|
||||
// 列字段转成 下划线 -> 小驼峰
|
||||
column.setFieldName(ConvertUtil.convertToFieldName(column.getColumnName()));
|
||||
column.setFieldName(ConvertUtil.convertToCamelCase(column.getColumnName()));
|
||||
|
||||
// 字段类型
|
||||
column.setJdbcType(columnsRs.getString("TYPE_NAME"));
|
||||
|
@ -166,7 +166,6 @@ public class DbInfoUtil {
|
|||
boolean isPrimaryKey = primaryKeyColumns.contains(columnName);
|
||||
column.setIsPrimaryKey(isPrimaryKey);
|
||||
}
|
||||
|
||||
columns.add(column);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package cn.bunny.utils;
|
||||
|
||||
import cn.bunny.dao.dto.VmsArgumentDto;
|
||||
import com.google.common.base.CaseFormat;
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
import org.apache.velocity.app.Velocity;
|
||||
|
@ -8,10 +9,19 @@ import org.apache.velocity.app.Velocity;
|
|||
import java.io.StringWriter;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
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"
|
||||
);
|
||||
|
||||
/**
|
||||
* 生成模板
|
||||
*
|
||||
|
@ -53,13 +63,52 @@ public class VmsUtil {
|
|||
context.put("requestMapping", requestMapping);
|
||||
|
||||
// 将类名称转成小驼峰
|
||||
context.put("classLowercaseName", ConvertUtil.convertToCamelCase(replaceTableName.get()));
|
||||
String toCamelCase = ConvertUtil.convertToCamelCase(replaceTableName.get());
|
||||
context.put("classLowercaseName", toCamelCase);
|
||||
|
||||
// 将类名称转成大驼峰
|
||||
context.put("classUppercaseName", ConvertUtil.convertToCamelCase(replaceTableName.get(), true));
|
||||
String convertToCamelCase = ConvertUtil.convertToCamelCase(replaceTableName.get(), true);
|
||||
context.put("classUppercaseName", convertToCamelCase);
|
||||
|
||||
// Velocity 生成模板
|
||||
Template servicePathTemplate = Velocity.getTemplate(templateName, "UTF-8");
|
||||
servicePathTemplate.merge(context, writer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理 vm 文件名
|
||||
*
|
||||
* @param path 文件路径
|
||||
* @param className 类名
|
||||
*/
|
||||
public static String handleVmFilename(String path, String className) {
|
||||
String[] splitPaths = path.split("/");
|
||||
int splitPathsSize = splitPaths.length - 1;
|
||||
|
||||
// 大驼峰名称
|
||||
String CamelCase = ConvertUtil.convertToCamelCase(className, true);
|
||||
// 小驼峰名称
|
||||
String camelCase = ConvertUtil.convertToCamelCase(className);
|
||||
|
||||
// 当前文件名
|
||||
String filename = splitPaths[splitPathsSize];
|
||||
filename = filename.replace(".vm", "");
|
||||
|
||||
// 文件名称
|
||||
String name = filename.split("\\.")[0];
|
||||
// 文件扩展名
|
||||
String extension = filename.split("\\.")[1];
|
||||
|
||||
// 判断是否是 Java 或者 xml 文件
|
||||
if (filename.contains("java") || filename.contains("xml")) {
|
||||
filename = CamelCase + TYPE_MAPPINGS.get(name) + "." + extension;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -84,7 +84,7 @@ public class JDBCTest {
|
|||
while (columnsRs.next()) {
|
||||
ColumnMetaData column = new ColumnMetaData();
|
||||
column.setColumnName(columnsRs.getString("COLUMN_NAME"));
|
||||
column.setFieldName(ConvertUtil.convertToFieldName(column.getColumnName()));
|
||||
column.setFieldName(ConvertUtil.convertToCamelCase(column.getColumnName()));
|
||||
column.setJdbcType(columnsRs.getString("TYPE_NAME"));
|
||||
column.setJavaType(ConvertUtil.convertToJavaType(column.getJdbcType()));
|
||||
column.setComment(columnsRs.getString("REMARKS"));
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package cn.bunny;
|
||||
|
||||
import cn.bunny.utils.ConvertUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.assertj.core.util.introspection.CaseFormatUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class StringFormatTest {
|
||||
|
||||
@Test
|
||||
void test1() {
|
||||
System.out.println(CaseFormatUtils.toCamelCase("user_login"));
|
||||
System.out.println(CaseFormatUtils.toCamelCase("userLogin"));
|
||||
System.out.println(CaseFormatUtils.toCamelCase("UserLogin"));
|
||||
|
||||
System.out.println("--------------------------------");
|
||||
|
||||
System.out.println(StringUtils.lowerCase("user_login"));
|
||||
System.out.println(StringUtils.lowerCase("userLogin"));
|
||||
System.out.println(StringUtils.lowerCase("UserLogin"));
|
||||
|
||||
System.out.println("--------------------------------");
|
||||
|
||||
System.out.println(StringUtils.upperCase("user_login"));
|
||||
System.out.println(StringUtils.upperCase("userLogin"));
|
||||
System.out.println(StringUtils.upperCase("UserLogin"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void test2() {
|
||||
System.out.println(ConvertUtil.convertToCamelCase("user_login_A"));
|
||||
System.out.println(ConvertUtil.convertToCamelCase("User_Login_A"));
|
||||
System.out.println(ConvertUtil.convertToCamelCase("userLoginA"));
|
||||
System.out.println(ConvertUtil.convertToCamelCase("UserLoginA"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue