实体对象转化成MAP,java代码示例
时间: 2023-09-30 12:12:34 浏览: 45
Sure, here's an example in Java for converting an entity object to a Map:
```
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
public class EntityMapper {
public static Map<String, Object> convertEntityToMap(Object entity) throws IllegalAccessException {
Class<?> clazz = entity.getClass();
Map<String, Object> map = new HashMap<>();
for(Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
String fieldName = field.getName();
Object fieldValue = field.get(entity);
map.put(fieldName, fieldValue);
}
return map;
}
}
```
You can use this method by passing in an instance of your entity object and it will return a Map with the field names and their corresponding values. Hope this helps!