前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用beanUtils操纵javabean

使用beanUtils操纵javabean

原创
作者头像
MonroeCode
发布2018-01-11 14:14:55
5900
发布2018-01-11 14:14:55
举报
package com.lan.beanutils;  
  
import java.lang.reflect.InvocationTargetException;  
import java.text.ParseException;  
import java.text.SimpleDateFormat;  
import java.util.Date;  
  
  
import org.apache.commons.beanutils.BeanUtils;  
import org.apache.commons.beanutils.ConversionException;  
import org.apache.commons.beanutils.ConvertUtils;  
import org.apache.commons.beanutils.Converter;  
import org.junit.Test;  
  
import sun.java2d.pipe.SpanShapeRenderer.Simple;  
  
//使用beanUtils操作bean的属性  
public class Demo {  
  
    @Test  
    public void test1() throws IllegalAccessException, InvocationTargetException{  
          
        Person person = new Person();  
          
        BeanUtils.setProperty(person, "name", "xxxx");  
          
        System.out.println(person.getName());  
    }  
      
    @Test  
    public void test2() throws IllegalAccessException, InvocationTargetException{  
          
        String nameString = "aaa";  
        String passwordString = "123";  
        String age = "33";  
        String birthdayString = "1988-01-01";  
          
        Person person = new Person();  
        BeanUtils.setProperty(person, "name", nameString);  
        BeanUtils.setProperty(person, "password", passwordString);  
        BeanUtils.setProperty(person, "age", age);//只支持8种基本数据类型自动转换  
        //BeanUtils.setProperty(person, "birthday", birthdayString);  
          
        System.out.println(person.getName());  
        System.out.println(person.getPassword());  
        System.out.println(person.getAge());  
        //System.out.println(person.getBirthday());  
          
          
        //为了让日期赋到bean的birthday属性上,我们给beanUtils注册一个日期转换器  
        ConvertUtils.register(new Converter() {  
              
            @Override  
            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);  
          
          
        BeanUtils.setProperty(person, "birthday", birthdayString);  
        System.out.println(person.getBirthday());  
  
                Date date = person.getBirthday();  
        System.out.println(date.toLocaleString());  
    }  
}  
package com.lan.beanutils;  
  
  
import java.util.Date;  
  
  
  
//javabean  
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;  
    }  
      
}  

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档