BeanUtils 读取数据

前两篇文章都是关于setProperty的,下面来说一个关于getProperty 的小案例。如下:

MyClass.java

package beanutils;

public class MyClass {

    private String classname;

    public String getClassname() {
        return classname;
    }

    public void setClassname(String classname) {
        this.classname = classname;
    }


}

MyBean.java

package beanutils;

import java.util.Date;

public class MyBean {

    private String name;
    private int age;
    private Date birthday;
    private boolean isAlive;

    private MyClass myclass;

    public MyClass getMyclass() {
        return myclass;
    }

    public void setMyclass(MyClass myclass) {
        this.myclass = myclass;
    }

    public MyBean() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    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;
    }

    public boolean isAlive() {
        return isAlive;
    }

    public void setAlive(boolean isAlive) {
        this.isAlive = isAlive;
    }


}

AdvanceBean.java

package beanutils;

import java.util.List;

/** * 提供了集合的形式获取 * * @author Administrator * */
public class AdvanceBean {

    private List list;

    private MyClass myclass;


    public MyClass getMyclass() {
        return myclass;
    }

    public void setMyclass(MyClass myclass) {
        this.myclass = myclass;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }


}

TestAdvanceBean.java

package beanutils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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.Test;

public class TestAdvanceBean {

    /** * 简单的测试基本数据类型的数据的getProperty方法 * * @throws Exception */
    @Test
    public void test1() throws Exception {

        String name = "Molly";
        int age = 20;
        Boolean isAlive = true;
        MyBean myBean = new MyBean();
        BeanUtils.setProperty(myBean, "name", name);
        BeanUtils.setProperty(myBean, "age", age);

        System.out.println("Name: " + BeanUtils.getProperty(myBean, "name"));
        System.out.println("Age: " + BeanUtils.getProperty(myBean, "age"));

    }

    /** * 对于boolean数据类型,由于其setter,getter方法不符合标准,所以不能正确的获得输出结果 * * java.lang.NoSuchMethodException: Unknown property 'isAlive' on class * 'class beanutils.MyBean' * * @throws Exception */
    @Test
    public void test2() throws Exception {
        MyBean myBean = new MyBean();
        boolean isAlive = false;
        BeanUtils.setProperty(myBean, "isAlive", isAlive);

        System.out.println("Name: " + BeanUtils.getProperty(myBean, "name"));
        System.out.println("Age: " + BeanUtils.getProperty(myBean, "age"));
        System.out.println("IsAlive: " + BeanUtils.getProperty(myBean, "isAlive"));
    }

    /** * 对Date数据类型<br> * 进行测试 使用内置的转换器 * * @throws Exception */
    @Test
    public void test3() throws Exception {
        String birthday = "2016-07-05";
        MyBean myBean = new MyBean();
        ConvertUtils.register(new DateLocaleConverter(), Date.class);
        BeanUtils.setProperty(myBean, "birthday", birthday);
        System.out.println(BeanUtils.getProperty(myBean, "birthday"));
    }

    /** * 对Date数据类型<br> * 进行测试 使用z自定义的转换器 * * @throws Exception */
    @Test
    public void test4() throws Exception {
        String birthday = "2016-07-05 11:59:59";
        MyBean myBean = new MyBean();
        ConvertUtils.register(new Converter() {

            @Override
            public <T> T convert(Class<T> type, Object value) {
                try {
                    String data = (String) value;
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    return (T) sdf.parse(data);
                } catch (Exception e) {
                    throw new RuntimeException(e.getMessage());
                }
            }

        }, Date.class);
        BeanUtils.setProperty(myBean, "birthday", birthday);
        System.out.println(BeanUtils.getProperty(myBean, "birthday"));
    }

    /** * 测试 组合类中的property获取 <br> * 支持内嵌类数据的读取,但是不支持内嵌类的直接的读取 * * @throws Exception */
    @Test
    public void test5() throws Exception {

        MyClass myclass = new MyClass();
        myclass.setClassname("软件学院软件开发与测试1414班");
        MyBean myBean = new MyBean();
        BeanUtils.setProperty(myBean, "myclass", myclass);
        System.out.println(BeanUtils.getProperty(myBean, "myclass.classname"));
    }

    /** * 对于list等数据的getProperty测试<br> * * 默认获得第一个列表项的内容,也可以指定下标进行内容的读取 * * @throws Exception */
    @Test
    public void test6() throws Exception {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < 7; i++) {
            list.add("列表项" + i);
        }
        AdvanceBean advanceBean = new AdvanceBean();
        BeanUtils.setProperty(advanceBean, "list", list);
        System.out.println(BeanUtils.getProperty(advanceBean, "list").toString());
        System.out.println(BeanUtils.getProperty(advanceBean, "list[1]").toString());
        System.out.println(BeanUtils.getProperty(advanceBean, "list[6]").toString());

    }

}

总结

代码很详细,就不再废话了。

最后一点需要提醒的是,导好包!导对包!思路很重要!

你可能感兴趣的:(BeanUtils,数据读取)