apache 自己为程序员们封装了一个专门用于处理的工具类,其功能有(数据类型会自动转成与JavaBean相关的)
map转javabean
javabean转map
javabean对象复制
获取javabean对象属性值
设置javabean对象属性值…………
两个相关jar包文件 Build Path到项目当中去
commons-beanutils-1.9.2.jar
commons-logging-1.2.jar
1.将Map转换成JavaBean对象
/**
* 刘诗华
*
@param
args
*
@throws
Exception
*/
public
static
void main(String[] args) throws Exception {
Map<String, Object> m=new HashMap<String, Object>();
m.put("id", "28");
m.put("userName", "刘诗华");
m.put("password", "123456");
User user=new User();
//BeanUtils.copyProperties(dest, orig); dest:目标 orig:源 BeanUtils.copyProperties(user,m);
System.out.println(user); //结果:User(id=28, userName=刘诗华, password=123456)
Integer id = user.getId(); //我们设置给Map集合的时候,给的是一个字符串,BeanUtils工具自动帮我们转换成包装类Integer类型 System.out.println(id);
}
原文:https://www.cnblogs.com/hua900822/p/9986330.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!