BeanUtils给属性赋值

首先使用:BeanUtils需要导入两个包:

1. commons-beanutils-1.9.3.jar
2. commons-logging-1.2.jar

如果只有前一个 没有后一个就会报错:

Exception in thread "main" java.lang  
.NoClassDefFoundError:org/apache/commons/logging/LogFactory

然后创建entity:

package entity;

import java.util.Date;

public class Teacher {
    private Integer id;
    private String name;
    private Date birthday;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    @Override
    public String toString() {
        return "teacher:["+id+","+name+","+birthday+"]";
    }
}

创建测试单元:

package test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.jupiter.api.Test;
import entity.Teacher;

class beanutils {

    @Test
    void test1() throws Exception {
        Teacher teacher = new Teacher();
        BeanUtils.copyProperty(teacher, "id", 12);
        BeanUtils.copyProperty(teacher, "name", "莉莉");
        System.out.println(teacher);
    }
    @Test
    void test2() throws Exception {
        Teacher teacher = new Teacher();
        BeanUtils.copyProperty(teacher, "id", 12);
        BeanUtils.copyProperty(teacher, "name", "伊恩");
        Teacher newTeacher = new Teacher();
        BeanUtils.copyProperties(newTeacher, teacher);
        System.out.println(newTeacher);
    }
    @Test
    void test3() throws Exception {
        Teacher teacher = new Teacher();
        Map map = new HashMap(2);
        map.put("name", "玛丽");
        map.put("id", 11);
        BeanUtils.populate(teacher, map);
        System.out.println(teacher);
    }
    @Test
    void test4() throws Exception {

        ConvertUtils.register(new Converter() {

            @SuppressWarnings("unchecked")
            @Override
            public  T convert(Class arg0, Object arg1) {
                // TODO Auto-generated method stub
                if(arg0!=Date.class) {
                    return null;
                }
                if(arg1==null||"".equals(arg1.toString().trim())) {
                    return null;
                }
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    return (T) simpleDateFormat.parse(arg1.toString());
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return null;
            }
        }, Date.class);

        Teacher teacher = new Teacher();
        BeanUtils.setProperty(teacher, "birthday", "1997-10-25");
        System.out.println(teacher);
    }

    @Test
    void test5() throws Exception {

        String pattern = "yyyy-MM-dd";
        //返回默认区域设置的Java虚拟机实例
        Locale locale = Locale.getDefault();

        DateLocaleConverter converter = new DateLocaleConverter(locale,pattern);
        converter.setLenient(true);
        ConvertUtils.register(converter, Date.class);
        Teacher teacher = new Teacher();
        BeanUtils.copyProperty(teacher, "id", 12);
        BeanUtils.copyProperty(teacher, "name", "伊恩");
        BeanUtils.setProperty(teacher, "birthday", "1999-09-19");

        System.out.println(teacher);
    }
}

给普通属性赋值:

//两个效果是一样的
BeanUtils.setProperty()
BeanUtils.copyProperty()

对象赋值对象:


BeanUtils.copyProperties(newTeacher, teacher);

map赋值对象:

BeanUtils.populate(teacher, map);

特殊属性 java.util.Date赋值:
两种方法:
第一种自定义:
第二种使用内置方法:

ConvertUtils.register(  
new DateLocaleConverter(locale,pattern), Date.class);

这里需要注意的是:
如果是:

ConvertUtils.register(  
new DateLocaleConverter(), Date.class);

就会报错:


org.apache.commons.beanutils.ConversionException:   
Error parsing date '1999-09-19' at position=4

百度找了很多 终于找到这个方法:

String pattern = "yyyy-MM-dd";
Locale locale = Locale.getDefault();
DateLocaleConverter converter = new DateLocaleConverter(locale,pattern);
converter.setLenient(true);
ConvertUtils.register(converter, Date.class);

注意:

//返回默认区域设置的Java虚拟机实例
Locale locale = Locale.getDefault();

/*比如说当你使用的时候有2012-02-31,2012-14-03这样数据  
去format,如果让setLenient(true).那么它就会自动解析为  
2012-03-022013-02-03这样的日期.*/
converter.setLenient(true);

你可能感兴趣的:(JAVA,Web基础)