如:http://www.funnymudpee.com/?name=xiaoming&age=18
把name=xiaoming&age=18转为Java数据
JavaBean | Map | |||
属性名 | 属性值 | key | value | |
name | xiaoming | name | xiaoming | |
age | 18 | age | 18 |
思路:把JavaBean中的属性和属性值分别拿出来,放到Map。
public static Map bean2map(Object obj) throws Exception {
Map map = new HashMap();
//使用内省,获取属性名和属性值
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass(), Object.class);
//获取属性描述器
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor pd : pds) {
String propertyName = pd.getName(); //属性名
//先获取get方法, 其执行后的返回值就是属性值
Object propertyValue = pd.getReadMethod().invoke(obj);
//属性为key,属性值为value
map.put(propertyName,propertyValue);
}
return map;
}
思路:因为Map的key和JavaBean中的属性是同名的,通过已知的Java对象,内省获得属性名。
把属性名作为map的key,获得map的value,最后再放到JavaBean中。
public static Object map2bean(Map map, Class clz) throws Exception{
//由传进来的参数决定JavaBean
Object obj = clz.newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(clz, Object.class);
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor pd : pds) {
//map.get(属性名)
String propertyName = pd.getName(); //属性名
//对应的JavaBean上的属性名和Map上的key是同名的
Object value = map.get(propertyName);
//获取set方法,然后执行
Method m = pd.getWriteMethod();
m.invoke(obj, value);
}
return obj;
}
补充测试结果:
package cn.xbao;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
public class BeanUtil {
//工具类: 构造器应该私有化或使用abstract修饰类
private BeanUtil(){}
public static Map bean2map(Object obj) throws Exception {
Map map = new HashMap();
//使用内省,获取属性名和属性值
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass(), Object.class);
//获取属性描述器
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor pd : pds) {
String propertyName = pd.getName(); //属性名
//先获取get方法, 其执行后的返回值就是属性值
Object propertyValue = pd.getReadMethod().invoke(obj);
//属性为key,属性值为value
map.put(propertyName,propertyValue);
}
return map;
}
public static Object map2bean(Map map, Class clz) throws Exception{
//由传进来的参数决定JavaBean
Object obj = clz.newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(clz, Object.class);
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor pd : pds) {
//map.get(属性名)
String propertyName = pd.getName(); //属性名
//对应的JavaBean上的属性名和Map上的key是同名的
Object value = map.get(propertyName);
//获取set方法,然后执行
Method m = pd.getWriteMethod();
m.invoke(obj, value);
}
return obj;
}
}
package cn.xbao;
import lombok.*;
import java.util.Map;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Student {
private String name;
private int age;
public static void main(String[] args) {
Student student = new Student();
student.setName("安琪拉");
student.setAge(18);
try {
Map map = BeanUtil.bean2map(student);
System.out.println(map);
System.out.println("-----------------");
Object o = BeanUtil.map2bean(map, student.getClass());
System.out.println(o);
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果: