/**
* 把map转成指定类型的JavaBean对象
* @param map
* @param clazz
* @return
*/
public static <T> T toBean(Map map,Class<T> clazz){
try {
/*
* 1创建指定类的javabean对象
*/
T bean=clazz.newInstance();
/**
* 2.把数据封装到Java bean中
*/
BeanUtils.populate(bean, map);
/**
* 3.返回Javabean;
*/
return bean;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Test
public void fun3(){
Map<String, String> map=new HashMap<String, String>();
map.put("username", "zhangsan");
map.put("password", "12345");
User user2=CommonUtils.toBean(map, User.class);
System.out.println(user2);
}