BeanUtils

一、概念:BeanUtils工具是一种方便我们对JavaBean进行操作的工具,是Apache组织下的产品。

二、导包:

  1commons-beanutils;

  2commons-logging;

  3commons-collections;//不行再导;

三、核心方法:

1)拷贝map属性:BeanUtils.populate(Object bean, Map<String,String[]>properties);

  //Map数据封装到指定Javabean中;

  //一般用于将表单的所有数据封装到javabean;

2)拷贝对象属性:BeanUtils.copyProperties(p,d);//把d中的属性拷贝到p中;

2)设置属性:BeanUtils.setProperty(Object obj,String name,Object value);//参数object为需要设置对象;

3)获取属性:BeanUtils.getProperty(Object obj,String name);

 四、示例:

public class BeanUtilsTest {
    public static void main(String[] args) throws Exception {
        Student student = new Student();
        Student student2 = new Student();
        student2.setSno("001");
        student2.setSname("zs");
        student2.setSsex("1");
        BeanUtils.setProperty(student, "sname", "ls");
        System.out.println(BeanUtils.getProperty(student, "sname"));
        //BeanUtils.copyProperties(student, student2);
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("sno", "002");
        hashMap.put("ssex", "2");
        BeanUtils.populate(student, hashMap);
        System.out.println(student);
    }
}

 

posted @ 2019-08-04 23:43  开拖拉机的拉风少年  阅读(247)  评论(0编辑  收藏  举报