<!-- BeanUtils的依赖 -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
设置和读取属性
//设置属性
Map<String, Object> map = new HashMap<>();
BeanUtils.setProperty(map, "name", "张三");
//读取属性
String name = BeanUtils.getProperty(map, "name");
System.out.println("name=" + name);
复制对象的所有属性
Student s1 = new Student();
s1.setAge(1);
s1.setName("张三");
Student s2 = new Student();
BeanUtils.copyProperties(s2, s1);
System.out.println(s2.getName() + ":" + s2.getAge());
//输出:张三:1
克隆对象(浅克隆)
Student s1 = new Student();
s1.setAge(1);
s1.setName("张三");
Student s2 = (Student) BeanUtils.cloneBean(s1);
System.out.println(s2.getName() + ":" + s2.getAge());
//输出:张三:1
将map数据拷贝到对象中
Map<String, Object> map = new HashMap<>();
map.put("name", "张三");
map.put("age", "1");
Student s = new Student();
BeanUtils.populate(s, map);
System.out.println(s.getName() + ":" + s.getAge());
//输出:张三:1
标签:map,包下,s2,s1,Student,apache,new,BeanUtils From: https://www.cnblogs.com/xfeiyun/p/17856110.html