java学习-BeanUtils给对象的属性赋值

需要加入两个jar包:

commons-beanutils-1.9.3.jar

commons-logging-1.2.jar

使用方法:

给对象的属性赋值:BeanUtils.setProperty(entity, fieldName, value);

从对象的属性中取值:BeanUtils.getProperty(entity, fieldName);

例子:

@Test
    public void testGetProperty() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
        Object student = new Student();
        BeanUtils.setProperty(student, "idCard", "20190099282929");
        Object obj = BeanUtils.getProperty(student, "idCard");
        System.out.println(obj);
    }
    @Test
    public void testSetProperty() throws IllegalAccessException, InvocationTargetException {
        Object student = new Student();
        BeanUtils.setProperty(student, "idCard", "20190099282929");
        System.out.println(student);
    }
原文地址:https://www.cnblogs.com/hjwq/p/7583177.html