BeanUtils用法

BeanUtils一套开发包,Apache公司提供 ,专门进行javabean操作,在web层各种框架中被使用,例如:struts 使用BeanUtils操作JavaBean 。

下载BeanUtils的jar :commons-beanutils 、commons-logging,需要同时下载两个jar包


用法一


Person person = new Person();

        // 将String类型 转换 java.util.Date类型 --- 自定义转换器
        // 在封装数据之前 ,注册转换器
        ConvertUtils.register(new MyDateConverter(), Date.class);

        try {
            BeanUtils.populate(person, request.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
用法二

  BeanUtils.setProperty(p, "id", id);  //其中p代表的是要设置的对象

        BeanUtils.setProperty(p, "name", name);   //中间一个参数代表的是要设置的属性

        BeanUtils.setProperty(p, "salary", salary); //第三个参数代表的是第二个属性的值


猜你喜欢

转载自blog.csdn.net/weixin_39719266/article/details/79763883