kongdeqiang
2023-06-08 3fba1d84220268d871c3c28e0e25f6eab3526f46
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.boying.util;
 
import org.springframework.stereotype.Component;
 
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
 
@Component
public class ObjectToMapUtil {
 
    public  static Map<String,Object> objectToMap(Object obj)throws IllegalAccessException{
        Map<String,Object> map = new HashMap<>();
        Class<?> aClass = obj.getClass();
        for (Field field : aClass.getDeclaredFields()) {
            field.setAccessible(true);
         //   System.out.println(field);
            String name = field.getName();
            Object o = field.get(obj);
            if (o == null)
                o = "";
            map.put(name, o);
        }
        return map;
    }
}