首页 > 其他 > 详细

BeanUtils

时间:2019-08-05 00:11:30      阅读:105      评论:0      收藏:0      [点我收藏+]

一、概念: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);
    }
}

 

BeanUtils

原文:https://www.cnblogs.com/Tractors/p/11300591.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!