由于JavaBean(实体类)结构与Map类似,我们可以把JavaBean与Map进行转换
package com.yy; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; /** * @java项目www.fhadmin.org * @description: 采用单例模式实现 * @date 2021 */ public class JavaBeanChangeMap { private static final JavaBeanChangeMap javaBeanChangeMap = new JavaBeanChangeMap(); private JavaBeanChangeMap() { } public static JavaBeanChangeMap getInstance() { return javaBeanChangeMap; } /** * JavaBean 转 Map */ public <T> Map<String, Object> JavaBeanToMap(Class<T> t) throws IntrospectionException, InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException { // 1、创建 Map 集合 Map<String, Object> map = new HashMap<>(); // 2、通过内省机制获取 PropertyDescriptors 属性数组 // Object.class PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(Person.class, Object.class) .getPropertyDescriptors(); Constructor<T> constructor = t.getConstructor(String.class, int.class); Object obj = constructor.newInstance("afa", 12); System.out.println("obj = " + obj); // 3、遍历该数组获取 Get 方法,获取属性名作为 key ,获取对象值作为 value for(PropertyDescriptor propertyDescriptor : propertyDescriptors) { String name = propertyDescriptor.getName(); Method readMethod = propertyDescriptor.getReadMethod(); Object invoke = readMethod.invoke(obj); map.put(name, invoke); } return map; } /** * Map 转 JavaBean */ public <T> T mapToJavaBean(Map map, Class<T> c) throws IllegalAccessException, InstantiationException, IntrospectionException, InvocationTargetException { // 1、通过字节码对象创建空的实例 T o = c.newInstance(); // 2、通过 Introspector 类把bean对象信息封装到 beanInfo 中 BeanInfo beanInfo = Introspector.getBeanInfo(c, Object.class); // 3、通过 getPropertyDescriptors() 获取一个属性(get/set)数组 PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); // 4、遍历该数组,把获取的名字作为 map 的 key,通过 key 取出对应的 value 值 for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { String key = propertyDescriptor.getName(); Object value = map.get(key); Method writeMethod = propertyDescriptor.getWriteMethod(); writeMethod.invoke(o, value); } return o; } }
Junit测试代码:
package com.yy.test; import com.yy.JavaBeanChangeMap; import com.yy.Person; import org.junit.Test; import java.beans.IntrospectionException; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; /** * @java项目www.fhadmin.org * @description: 测试 * @date 2021/4/10 */ public class JavaBeanChangeMapTest { @Test public void testJavaBeanToMap() throws Exception { Map<String, Object> map = JavaBeanChangeMap.getInstance().JavaBeanToMap(Person.class); System.out.println("map = " + map); System.out.println("================================"); } @Test public void testMapToJavaBean() throws Exception { Map map = new HashMap(); map.put("name", "afa"); map.put("age", 12); Person person = JavaBeanChangeMap.getInstance().mapToJavaBean(map, Person.class); System.out.println("person = " + person); } }
运行结果:
"C:\Program Files\Java\jdk-11.0.9\bin\java.exe" obj = Person{name='afa', age=12} map = {name=afa, age=12} ================================ person = Person{name='afa', age=12} Process finished with exit code 0