虽然通过反射也可以,但是可能稍微有点麻烦,如何将实体对象快速转换成 Map 呢?接下来使用 net.sf.cglib.beans.BeanMap 类中方法进行转换
People people = new People(); people.setName(" 葬爱 "); people.setAge(18); BeanMap beanMap = BeanMap.create(people); System.out.println(beanMap); // 输出 {name= 葬爱, age=18} Map beanMap1 = beanMap; System.out.println(beanMap1);// 输出 {name= 葬爱, age=18} HashMap<Object, Object> map = new HashMap<>(); map.put("name"," 韩梅梅 "); map.put("age",17); map.put("sex","felman"); beanMap.putAll(map); System.out.println(beanMap); //{name= 韩梅梅, age=17} People bean = (People) beanMap.getBean(); System.out.println(bean);//People{name=' 韩梅梅 ', age=17}