BeanUtils与PropertyUtils区别

相同的: 都是浅拷贝,都提供了copyProperties()方法,只要属性名相同就可以从源bean中拷贝值到目标bean中
不同点: BeanUtils.copyProperties提供类型转换功能,BeanUtils会调用默认的转换器(Converter)进行类型转换,所以在拷贝时能对八个基本类型间进行转换,不能转换时抛出错误
PropertyUtils.copyProperties不提供类型转换功能,即发现两个JavaBean的同名属性为不同类型时,会提示argument mistype异常。

PropertyUtils.copyProperties(Object dest, Object orig);

实体间拷贝

User user = new User();
user.setName("张三");
user.setAge(24);
User user2 = new User();
PropertyUtils.copyProperties(user2, user);
System.out.println(user2);//User [name=张三, age=24]
   
   

map和实体间拷贝

User user3 = new User();
Map<String,Object> map = new HashMap<String, Object>();
map.put("name", "王五");
map.put("age", 25);
PropertyUtils.copyProperties(user3, map);
System.out.println(user3);//User [name=王五, age=25]
   
   原文地址:https://blog.csdn.net/u012894692/article/details/80087859

猜你喜欢

转载自www.cnblogs.com/jpfss/p/12110826.html