/**
将map中的实体类反射出来
*/
public static <T> T map2Object(Map<String, Object> map, Class<T> clazz) {
Object obj ;
try {
obj = clazz.newInstance();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
ConvertUtils.register(new Converter() {
public Object convert(Class type, Object value) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
if(value==null){
return null;
}else {
return simpleDateFormat.parse(value.toString());
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}, Date.class);
for (Object o : map.keySet()) {
//获得参数名
String name = String.valueOf(o);
//获得参数值
Object value = map.get(name);
//然后把参数拷贝到javaBean对象中
try {
if(value!=null){//空值不处理,否则会报空指针异常
BeanUtils.setProperty(obj, name, value);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
return (T) obj;
}