java中使用beanUtils操纵javabean

pom依赖的jar包配置:

<dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.3</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>

实例代码:

-----------------------------------------------------------------------------------------
package cn.it.cast.beanutil;
import java.util.Date;
public class Person {
    private String name;
    private String password;
    private int age;
    private Date birthday;
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    public int getAge() {
        return age;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

}
--------------------------------------------------------------------------------------------

测试代码:

//使用beanutils操纵bean的属性(第三方,apache)
public class Demo {
    Person p=new Person();
    @Test
    //给name赋值
    public void test1() throws Throwable, InvocationTargetException{
        BeanUtils.setProperty(p, "name","nihao" );
        System.out.println(p.getName());        
    }
    @Test
    //多个的赋值,支持基本类型
    public void test2() throws Throwable, InvocationTargetException{
        String name="qqq";
        String password ="123";
        String age="23";
        String birthday="1980-12-21";
        //为了让日志赋值到bean的birthday属性上,需要给beanUtils注册一个日期转换器
    /*    ConvertUtils.register(new Converter() {
            public Object convert(Class type, Object value) {
                if(value==null){
                return null;
                }
                if(!(value instanceof String)){
                    throw new ConversionException("指支持String类型的转化");
                    }
                String str =(String)value;
                if(str.trim().equals("")){
                    return null;
                }
                SimpleDateFormat df =new SimpleDateFormat("yyyy-MM-dd");
                try{
                return df.parse(str);
                }catch (ParseException e){
                    throw new RuntimeException(e);
                }
            }
        }, Date.class);*/
        
        ConvertUtils.register(new DateLocaleConverter(), Date.class);
        
        BeanUtils.setProperty(p, "name",name );
        BeanUtils.setProperty(p, "password",password );
        BeanUtils.setProperty(p, "age",age );
        BeanUtils.setProperty(p, "birthday", birthday);
        System.out.println(p.getName());
        System.out.println(p.getPassword());
        System.out.println(p.getAge());
        Date date=p.getBirthday();//得到Sun Dec 21 00:00:00 CST 1980
        System.out.println(date.toLocaleString());//日期转成中国的:1980-12-21 0:00:00
        
    }
    @Test
    public void test3() throws Throwable, InvocationTargetException{
        //使用map的方式初始化数据
        Map map=new HashMap();
        map.put("name", "jianjian");
        map.put("password", "123456");
        map.put("age", "30");
        map.put("birthday", "1987-07-30");
        //为了让日志赋值到bean的birthday属性上,需要给beanUtils注册一个日期转换器
        ConvertUtils.register(new DateLocaleConverter(), Date.class);
        BeanUtils.populate(p, map);
        System.out.println(p.getName());
        System.out.println(p.getPassword());
        System.out.println(p.getAge());
        Date date=p.getBirthday();//得到Sun Dec 21 00:00:00 CST 1980
        System.out.println(date.toLocaleString());
        
    }
}

posted @ 2016-12-13 20:07  贱贱的小帅哥  阅读(285)  评论(0)    收藏  举报