Java 实体对象与Map之间的转换工具类

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
 
public class EntityUtils {
	/**
	 * 实体类转Map
	 * @param object
	 * @return
	 */
	public static Map entityToMap(Object object) {
		Map map = new HashMap();
	    for (Field field : object.getClass().getDeclaredFields()){
	        try {
	        	boolean flag = field.isAccessible();
	            field.setAccessible(true);
	            Object o = field.get(object);
	            map.put(field.getName(), o);
	            field.setAccessible(flag);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	    }
	    return map;
	}
	
	/**
	 * Map转实体类
	 * @param map 需要初始化的数据,key字段必须与实体类的成员名字一样,否则赋值为空
	 * @param entity  需要转化成的实体类
	 * @return
	 */
	public static  T mapToEntity(Map map, Class entity) {
		T t = null;
		try {
			t = entity.newInstance();
			for(Field field : entity.getDeclaredFields()) {
				if (map.containsKey(field.getName())) {
					boolean flag = field.isAccessible();
		            field.setAccessible(true);
		            Object object = map.get(field.getName());
		            if (object!= null && field.getType().isAssignableFrom(object.getClass())) {
		            	 field.set(t, object);
					}
		            field.setAccessible(flag);
				}
			}
			return t;
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return t;
	}
}

 


来源于:

https://blog.csdn.net/m940034240/article/details/81118091

你可能感兴趣的:(------【工具类】,Java)