使用BeanUtils组件

时间:2024-04-29 16:00:03

使用BeanUtils组件 前提

1:导入commons-beanutils-1.8.3.jar        //根据  本人使用的是1.8.3的版本

    2:导入日志包      //就是logging包

有个User类

User user=new User();   //创建个user对象

BeanUtils.copyProperty(user,属性名,属性值);  //将user的属性名赋值        必须有setXxx方法;

BeanUtils.setProperty(user,属性名,属性值);   //这种方法结果同上

BeanUtils.copyProperties(user,新的User对象);   //对象的拷贝

//map的数据  拷贝到对象中

Map<String,Object>map =new HashMap<String,Object>();

map.put("属性名","属性值");    //map的key值  也就是属性名  要与javaBean(本例的User)的属性名一致

BeanUtils.populate(User对象,map);