package cn.huge.module.grid;
|
|
import java.lang.reflect.Field;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
public class DTOConverter {
|
public static Map<String, String> convertToMap(Object dto) {
|
Map<String, String> map = new HashMap<>();
|
// 获取该对象的所有字段(包括私有字段)
|
Field[] fields = dto.getClass().getDeclaredFields();
|
|
for (Field field : fields) {
|
try {
|
field.setAccessible(true); // 允许访问私有字段
|
Object value = field.get(dto); // 获取字段值
|
if (value != null) {
|
map.put(field.getName(), value.toString()); // 放入 Map 中
|
} else {
|
map.put(field.getName(), null); // 如果字段值为 null
|
}
|
} catch (IllegalAccessException e) {
|
e.printStackTrace(); // 处理反射异常
|
}
|
}
|
|
return map;
|
|
}
|
}
|