M2Bgo.java import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.util.Map;public class M2Bgo {/** * @return 直接返回JavaBean * @param map * 放入你的Map * @param clazz * 放入你的JavaBean.Cl
import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util.Map; public class M2Bgo { /** * @returnDemoVo.java直接返回JavaBean * @param map * 放入你的Map * @param clazz * 放入你的JavaBean.Class * @throws 各种异常爱接不接 * * @author MrXionGe * **/ public static T map2Bean(Map map, Class clazz) { T obj = null; try { obj = clazz.newInstance(); } catch (InstantiationException | IllegalAccessException e) { e.printStackTrace(); } if (map != null && map.size() > 0) { for (Map.Entry entry : map.entrySet()) { String propertyName = entry.getKey(); Object value = entry.getValue(); String setMethodName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); Field field = getClassField(clazz, propertyName); if (field == null) continue; Class fieldTypeClass = field.getType(); value = convertValType(value, fieldTypeClass); try { clazz.getMethod(setMethodName, field.getType()).invoke(obj, value); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { e.printStackTrace(); } } } return obj; } private static Field getClassField(Class clazz, String fieldName) { if (Object.class.getName().equals(clazz.getName())) { return null; } Field[] declaredFields = clazz.getDeclaredFields(); for (Field field : declaredFields) { if (field.getName().equals(fieldName)) { return field; } } Class superClass = clazz.getSuperclass(); if (superClass != null) { return getClassField(superClass, fieldName); } return null; } private static Object convertValType(Object value, Class fieldTypeClass) { Object retVal = null; if (Long.class.getName().equals(fieldTypeClass.getName()) || long.class.getName().equals(fieldTypeClass.getName())) { retVal = Long.parseLong(value.toString()); } else if (Integer.class.getName().equals(fieldTypeClass.getName()) || int.class.getName().equals(fieldTypeClass.getName())) { retVal = Integer.parseInt(value.toString()); } else if (Float.class.getName().equals(fieldTypeClass.getName()) || float.class.getName().equals(fieldTypeClass.getName())) { retVal = Float.parseFloat(value.toString()); } else if (Double.class.getName().equals(fieldTypeClass.getName()) || double.class.getName().equals(fieldTypeClass.getName())) { retVal = Double.parseDouble(value.toString()); } else { retVal = value; } return retVal; } }
public class DemoVo { private Integer id; private String name; private Integer age; private String addr; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } @Override public String toString() { return "DemoVo [id=" + id + ", name=" + name + ", age=" + age + ", addr=" + addr + "]"; } }DemoRun.java
import java.util.HashMap; import java.util.Map; public class DemoRun { public static void main(String[] args) { Mapmap = new HashMap<>(); map.put("id", "1701"); map.put("name", "ZhangSan"); map.put("age", "24"); map.put("addr", "Beijing"); System.out.println("Map----" + map); DemoVo vo = M2Bgo.map2Bean(map, DemoVo.class); System.out.println("Obj----" + vo); } }