实体类转成Map
一个将实体类转成Map映射的工具类。
代码如下:
public static<T> Map<String, Object> entityToMap(T obj) throws IllegalAccessException{
if (obj == null) {
return null;
}
Map<String, Object> map = new HashMap<>();
Field[] declaredFields = obj.getClass().getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
map.put(field.getName(), field.get(obj));
}
return map;
}