工具类:反射实现
使用范围:JavaBean类对象的属性不能是数组、List、Set、Map
public class MapBeanUtil {
/**
* JavaBean转Map
* @param obj
* @return
*/
public static Map bean2Map(Object obj) {
Map map = new LinkedHashMap();
Class clazz = obj.getClass();
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
String fieldName = field.getName();
Object value = null;
try {
value = field.get(obj);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (value == null){
value = "";
}
map.put(fieldName, value);
}
return map;
}
/**
* Map转JavaBean
* @param clazz
* @param map
* @param
* @return
*/
public static T map2Bean(final Class clazz, final Map map) {
if (map == null) {
return null;
}
T res = null;
try {
res = clazz.getDeclaredConstructor().newInstance();
//获取到所有属性,不包括继承的属性
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
//获取字段的修饰符
int mod = field.getModifiers();
if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
continue;
}
//设置对象的访问权限
field.setAccessible(true);
//根据属性名称去map获取value
if(map.containsKey(field.getName())) {
//给对象赋值
field.set(res, map.get(field.getName()));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
public static void main(String[] args) throws Exception {
HashMap map = new HashMap();
map.put("id", 1001);
map.put("username", "zhangsan");
map.put("password", "123456");
map.put("nickname", "张三");
map.put("email", "369950806@qq.com");
map.put("gender", true);
map.put("birth", LocalDate.now());
map.put("avatar", "/aa/bb/ab.jpg");
map.put("role", "VIP");
map.put("status", (byte) 1);
map.put("salt", "ldfkasjghweoiq324");
map.put("createTime", LocalDateTime.now());
map.put("updateTime", LocalDateTime.now());
User user = map2Bean(User.class, map);
System.out.println(user);
Map res = bean2Map(user);
System.out.println(map);
}
}
User类的代码:
public class User {
private Integer id;
private String username;
private String password;
private String nickname;
private String email;
private Boolean gender;
private LocalDate birth;
private String avatar;
private String role;
private Byte status;
private String salt;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}
工具类:反省实现
public class MapBeanTransUtil {
public static T map2Bean(Map map, Class clazz) throws IntrospectionException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException {
T obj = clazz.getDeclaredConstructor().newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(clazz, Object.class);
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor pd : pds) {
String field = pd.getName();
// System.out.println(field);
Method writeMethod = pd.getWriteMethod();
writeMethod.invoke(obj, map.get(field));
}
return obj;
}
public static Map bean2Map(Object obj) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
Map map = new HashMap();
//获取对象的字节码
Class clazz = obj.getClass();
BeanInfo beanInfo = Introspector.getBeanInfo(clazz, Object.class);
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor pd : pds) {
String field = pd.getName();
// System.out.println(field);
Method readMethod = pd.getReadMethod();
Object value = readMethod.invoke(obj);
// System.out.println(value);
map.put(field,value);
}
return map;
}
public static void main(String[] args) throws Exception {
//将Map转换成Java对象
Map map= new HashMap();
map.put("id",1001L);
map.put("name","zhangsan");
map.put("gender",true);
map.put("email","369950806@qq.com");
map.put("tel","13264494458");
map.put("credits",88);
map.put("status",3);
//内省
Class clazz = User.class;
User user = MapBeanTransUtil.map2Bean(map, clazz);
System.out.println(user);
System.out.println("***");
Map res = MapBeanTransUtil.bean2Map(user);
res.forEach((k,v)-> System.out.println(k+" - "+v));
}
}