fix: 修复转大驼峰命名失败

This commit is contained in:
bunny 2025-04-23 12:37:16 +08:00
parent 16a8170e86
commit 8f3ea1e87f
5 changed files with 60 additions and 55 deletions

View File

@ -3,8 +3,6 @@ package cn.bunny.core;
import com.google.common.base.CaseFormat;
import org.assertj.core.util.introspection.CaseFormatUtils;
import java.util.regex.Pattern;
/* 类型转换数据库转Java类型等 */
public class TypeConvertCore {
@ -16,17 +14,17 @@ public class TypeConvertCore {
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";
};
}
@ -49,27 +47,15 @@ public class TypeConvertCore {
public static String convertToCamelCase(String name, boolean firstLetterCapital) {
if (name == null || name.isEmpty()) return name;
// 转成小驼峰
String lowerCamelCase = CaseFormatUtils.toCamelCase(name);
// 首字母不大写
if (!firstLetterCapital) return CaseFormatUtils.toCamelCase(name);
// 检查是否全大写带下划线 (UPPER_UNDERSCORE)
if (Pattern.matches("^[A-Z]+(_[A-Z]+)*$", name)) {
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name);
if (!firstLetterCapital) {
return lowerCamelCase;
}
// 检查是否小写带下划线 (LOWER_UNDERSCORE)
if (Pattern.matches("^[a-z]+(_[a-z]+)*$", name)) {
return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name);
}
// 检查是否大驼峰 (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);
// 将小驼峰转成大驼峰
return CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, lowerCamelCase);
}
}

View File

@ -8,11 +8,11 @@ import java.util.Map;
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"
);
/**
@ -22,19 +22,20 @@ 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;
// 大驼峰名称
String CamelCase = TypeConvertCore.convertToCamelCase(className, true);
// 小驼峰名称
String camelCase = TypeConvertCore.convertToCamelCase(className);
System.out.println("CamelCase" + CamelCase);
System.out.println("camelCase" + camelCase);
// 当前文件名
String filename = splitPaths[splitPathsSize];
filename = filename.replace(".vm", "");
filename = filename.replace(".vm" , "" );
String[] split = filename.split("\\.");
String[] split = filename.split("\\." );
// 文件名称
String name = split[0];
// 文件扩展名
@ -46,15 +47,15 @@ public class VmsUtil {
// 判断是否是 Java 或者 xml 文件
String typeMappingsFilename = TYPE_MAPPINGS.get(name);
typeMappingsFilename = typeMappingsFilename == null ? "" : typeMappingsFilename;
if (filename.contains("java") || filename.contains("xml")) {
if (filename.contains("java" ) || filename.contains("xml" )) {
filename = CamelCase + typeMappingsFilename + "." + 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);
}
}

View File

@ -9,28 +9,35 @@ 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(CaseFormatUtils.toCamelCase("user_login" ));
System.out.println(CaseFormatUtils.toCamelCase("userLogin" ));
System.out.println(CaseFormatUtils.toCamelCase("UserLogin" ));
System.out.println("--------------------------------");
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(StringUtils.lowerCase("user_login" ));
System.out.println(StringUtils.lowerCase("userLogin" ));
System.out.println(StringUtils.lowerCase("UserLogin" ));
System.out.println("--------------------------------");
System.out.println("--------------------------------" );
System.out.println(StringUtils.upperCase("user_login"));
System.out.println(StringUtils.upperCase("userLogin"));
System.out.println(StringUtils.upperCase("UserLogin"));
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(TypeConvertCore.convertToCamelCase("user_login_A"));
System.out.println(TypeConvertCore.convertToCamelCase("User_Login_A"));
System.out.println(TypeConvertCore.convertToCamelCase("userLoginA"));
System.out.println(TypeConvertCore.convertToCamelCase("UserLoginA"));
System.out.println(TypeConvertCore.convertToCamelCase("user_login_A" ));
System.out.println(TypeConvertCore.convertToCamelCase("User_Login_A" ));
System.out.println(TypeConvertCore.convertToCamelCase("userLoginA" ));
System.out.println(TypeConvertCore.convertToCamelCase("UserLoginA" ));
System.out.println("--------------------------------" );
System.out.println(TypeConvertCore.convertToCamelCase("i18n_type_A" , true));
System.out.println(TypeConvertCore.convertToCamelCase("User_Login_A" , true));
System.out.println(TypeConvertCore.convertToCamelCase("userLoginA" , true));
System.out.println(TypeConvertCore.convertToCamelCase("UserLoginA" , true));
}
}

View File

@ -0,0 +1,8 @@
{
"hash": "727e0168",
"configHash": "10198a12",
"lockfileHash": "530f8857",
"browserHash": "41b39cb9",
"optimized": {},
"chunks": {}
}

View File

@ -0,0 +1,3 @@
{
"type": "module"
}