Files
saas-houduan/ruoyi-rocketmq/src/main/java/com/ruoyi/rocketmq/utils/FieldNameConverterUtil.java

73 lines
2.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.ruoyi.rocketmq.utils;
import org.apache.commons.lang3.StringUtils;
/**
* 数据库字段名转换工具类
*/
public class FieldNameConverterUtil {
/**
* 将数据库字段名转换为Java属性名带Collect后缀
* 示例system.swap.size.free -> systemSwapSizeFreeCollect
*
* @param dbFieldName 数据库字段名
* @return Java属性名
*/
public static String convertToJavaProperty(String dbFieldName) {
return convertToJavaProperty(dbFieldName, "Collect");
}
/**
* 将数据库字段名转换为Java属性名自定义后缀
* 示例system.swap.size.free + "Config" -> systemSwapSizeFreeConfig
*
* @param dbFieldName 数据库字段名
* @param suffix 自定义后缀
* @return Java属性名
*/
public static String convertToJavaProperty(String dbFieldName, String suffix) {
if (StringUtils.isBlank(dbFieldName)) {
return "";
}
// 分割字段名
String[] parts = dbFieldName.split("\\.");
// 处理每个部分
StringBuilder result = new StringBuilder(parts[0]);
for (int i = 1; i < parts.length; i++) {
result.append(StringUtils.capitalize(parts[i]));
}
// 添加后缀
if (StringUtils.isNotBlank(suffix)) {
result.append(suffix);
}
return result.toString();
}
/**
* 将Java属性名转换回数据库字段名
* 示例systemSwapSizeFreeCollect -> system.swap.size.free
*
* @param javaProperty Java属性名
* @param suffix 需要去除的后缀
* @return 数据库字段名
*/
public static String convertToDbField(String javaProperty, String suffix) {
if (StringUtils.isBlank(javaProperty)) {
return "";
}
// 去除后缀
String withoutSuffix = StringUtils.isNotBlank(suffix)
? StringUtils.removeEnd(javaProperty, suffix)
: javaProperty;
// 使用正则表达式在大小写字母之间插入点号
return withoutSuffix.replaceAll("(?<=[a-z])(?=[A-Z])", ".")
.toLowerCase();
}
}