..
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
package com.budwk.app.base.utils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 字段映射工具类
|
||||
* 支持多种命名规范的字段匹配和转换
|
||||
*/
|
||||
public class FieldMappingUtil {
|
||||
|
||||
/**
|
||||
* 多种方式获取字段,支持不同命名规范
|
||||
* @param clazz 类
|
||||
* @param columnName 列名或字段名
|
||||
* @return Field对象,找不到返回null
|
||||
*/
|
||||
public static Field getField(Class<?> clazz, String columnName) {
|
||||
if (clazz == null || columnName == null || columnName.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 1. 直接匹配(完全相同)
|
||||
Field field = getFieldDirectly(clazz, columnName);
|
||||
if (field != null) {
|
||||
return field;
|
||||
}
|
||||
|
||||
// 2. 驼峰命名匹配
|
||||
String camelName = underlineToCamel(columnName);
|
||||
if (!camelName.equals(columnName)) {
|
||||
field = getFieldDirectly(clazz, camelName);
|
||||
if (field != null) {
|
||||
return field;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 下划线命名匹配
|
||||
String underlineName = camelToUnderline(columnName);
|
||||
if (!underlineName.equals(columnName)) {
|
||||
field = getFieldDirectly(clazz, underlineName);
|
||||
if (field != null) {
|
||||
return field;
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 忽略大小写匹配
|
||||
field = getFieldIgnoreCase(clazz, columnName);
|
||||
if (field != null) {
|
||||
return field;
|
||||
}
|
||||
|
||||
// 5. 驼峰转下划线后忽略大小写匹配
|
||||
if (!underlineName.equals(columnName)) {
|
||||
field = getFieldIgnoreCase(clazz, underlineName);
|
||||
if (field != null) {
|
||||
return field;
|
||||
}
|
||||
}
|
||||
|
||||
// 6. 下划线转驼峰后忽略大小写匹配
|
||||
if (!camelName.equals(columnName)) {
|
||||
field = getFieldIgnoreCase(clazz, camelName);
|
||||
if (field != null) {
|
||||
return field;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接获取字段(完全匹配)
|
||||
*/
|
||||
private static Field getFieldDirectly(Class<?> clazz, String fieldName) {
|
||||
try {
|
||||
return clazz.getDeclaredField(fieldName);
|
||||
} catch (NoSuchFieldException e) {
|
||||
// 继续向上查找父类
|
||||
Class<?> superclass = clazz.getSuperclass();
|
||||
if (superclass != null && superclass != Object.class) {
|
||||
return getFieldDirectly(superclass, fieldName);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 忽略大小写获取字段
|
||||
*/
|
||||
private static Field getFieldIgnoreCase(Class<?> clazz, String fieldName) {
|
||||
Field[] fields = getAllFields(clazz);
|
||||
for (Field field : fields) {
|
||||
if (field.getName().equalsIgnoreCase(fieldName)) {
|
||||
return field;
|
||||
}
|
||||
}
|
||||
|
||||
// 再尝试驼峰和下划线转换后忽略大小写
|
||||
String camelName = underlineToCamel(fieldName);
|
||||
String underlineName = camelToUnderline(fieldName);
|
||||
|
||||
for (Field field : fields) {
|
||||
String fieldNameLower = field.getName().toLowerCase();
|
||||
if (fieldNameLower.equals(fieldName.toLowerCase()) ||
|
||||
fieldNameLower.equals(camelName.toLowerCase()) ||
|
||||
fieldNameLower.equals(underlineName.toLowerCase())) {
|
||||
return field;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取类及其父类的所有字段
|
||||
*/
|
||||
private static Field[] getAllFields(Class<?> clazz) {
|
||||
List<Field> fieldList = new ArrayList<>();
|
||||
Class<?> currentClass = clazz;
|
||||
|
||||
while (currentClass != null && currentClass != Object.class) {
|
||||
fieldList.addAll(Arrays.asList(currentClass.getDeclaredFields()));
|
||||
currentClass = currentClass.getSuperclass();
|
||||
}
|
||||
|
||||
return fieldList.toArray(new Field[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 下划线转驼峰
|
||||
*/
|
||||
public static String underlineToCamel(String underlineStr) {
|
||||
if (underlineStr == null || !underlineStr.contains("_")) {
|
||||
return underlineStr;
|
||||
}
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
String[] parts = underlineStr.toLowerCase().split("_");
|
||||
|
||||
for (int i = 0; i < parts.length; i++) {
|
||||
if (i == 0) {
|
||||
result.append(parts[i]);
|
||||
} else {
|
||||
result.append(capitalize(parts[i]));
|
||||
}
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 驼峰转下划线
|
||||
*/
|
||||
public static String camelToUnderline(String camelStr) {
|
||||
if (camelStr == null || camelStr.isEmpty()) {
|
||||
return camelStr;
|
||||
}
|
||||
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (int i = 0; i < camelStr.length(); i++) {
|
||||
char c = camelStr.charAt(i);
|
||||
if (Character.isUpperCase(c)) {
|
||||
if (i > 0) {
|
||||
result.append("_");
|
||||
}
|
||||
result.append(Character.toLowerCase(c));
|
||||
} else {
|
||||
result.append(c);
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 首字母大写
|
||||
*/
|
||||
private static String capitalize(String str) {
|
||||
if (str == null || str.isEmpty()) {
|
||||
return str;
|
||||
}
|
||||
return str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.budwk.app.base.utils;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.budwk.app.sys.models.Sys_menu;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 菜单工具类
|
||||
*/
|
||||
public class SysMenuUtil {
|
||||
|
||||
/**
|
||||
* 前端菜单数据
|
||||
*
|
||||
* @param menus
|
||||
* @param parentId
|
||||
* @return
|
||||
*/
|
||||
public static List<Sys_menu> createTreeMenus(List<Sys_menu> menus, String parentId) {
|
||||
List<Sys_menu> filterMenus = menus.stream().filter(v -> StringUtils.defaultString(v.getParentId(), "").equals(StringUtils.defaultString(parentId, ""))).collect(Collectors.toList());
|
||||
for (Sys_menu menu : filterMenus) {
|
||||
List<Sys_menu> childMenus = createTreeMenus(menus, menu.getId());
|
||||
menu.setChildren(childMenus);
|
||||
}
|
||||
return new ArrayList<>(filterMenus);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取菜单的根菜单
|
||||
* @param menus
|
||||
* @param menu
|
||||
* @return
|
||||
*/
|
||||
public static Sys_menu getRootMenu(List<Sys_menu> menus, Sys_menu menu) {
|
||||
// 如果当前菜单已经是根菜单(parentId为null),直接返回
|
||||
if (StrUtil.isBlank(menu.getParentId())) {
|
||||
return menu;
|
||||
}
|
||||
|
||||
// 查找当前菜单的父菜单
|
||||
Sys_menu parentMenu = menus.stream()
|
||||
.filter(m -> m.getId().equals(menu.getParentId()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
|
||||
// 如果找不到父菜单,说明数据有问题,返回当前菜单
|
||||
if (parentMenu == null) {
|
||||
return menu;
|
||||
}
|
||||
|
||||
// 递归查找父菜单的根菜单
|
||||
return getRootMenu(menus, parentMenu);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user