The map object reflected to the corresponding entity class

/**
将map中的实体类反射出来
*/
public static T map2Object(Map<String, Object> map, Class 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 ()) {
// get the parameter name
String name = String.valueOf (O);
// get the parameter value
Object value = map.get (name);
// then the parameters are copied to a subject javaBean
the try {
IF (= null value!) {// value is not null, otherwise a null pointer exception will be reported
BeanUtils.setProperty (obj, name, value);
}
} the catch (Exception E) {
e.printStackTrace ();
the throw a RuntimeException new new (E);
}
}
return (T) obj;
}

Published 24 original articles · won praise 1 · views 2447

Guess you like

Origin blog.csdn.net/qq_35018214/article/details/102695120