使用BeanUtils操作Javabean

  最近学习使用到了BeanUtils对Javabean进行一些操作和处理,回想从安装BeanUtils到使用其对Javabean进行操作处理和运用,此处做个简单的总结:

  首先是下载BeanUtils工具包,注意需要下载logging.jar并导入后才可使程序正常运行:

  1.登录https://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi

  2.在下载页面中的Binaries中选择一个,例如commons-beanutils-1.9.3-bin.tar.gz,点击下载即可。

  3.登录https://commons.apache.org/proper/commons-logging/download_logging.cgi

  4.在下载页面中的Binaries中选择一个,例如commons-logging-1.2-bin.tar.gz,点击下载即可。

  然后在IDE中导入jar包:

  个人偏爱使用Intellij IDEA,这里说说在idea中导入这两个jar包,打开IDEA的界面后,点击“File---->Project Structure---->Modules”,然后点击中间的<Modules source>然后点击右边的“+”号选择你所保存jar包的路径添加jar包,需要添加:commons-beanutils-1.9.3.jar和commons-logging-1.2.jar,点击Apply和OK即可。



然后这里简单应用一下Beanutils,将数据填充至bean中,代码中有使用自定义转换器和使用BeanUtils中的转换器:

先上bean的代码:

package beanUtils;

import java.util.Date;

/**
 * Created by jiangxs on 2017/9/27.
 */
public class Person {

    private String name;
    private int number;
    private int age;
    private Date birthday;

    public String getName() {
        return name;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

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

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getadc(){
        return "abc";
    }
}

下面是使用BeanUtils的代码示例:

package beanUtils;


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.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;

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

/**
 * Created by jiangxs on 2017/9/27.
 */
public class Demo1 {

    //使用BeanUtils操作bean并修改bean中数据
    @Test
    public void test1() throws Exception{
        Person p = new Person();
        BeanUtils.setProperty(p,"name","jxs");
        System.out.println(p.getName());
    }

    //使用BeanUtils将八种基本数据类型填充至bean
    @Test
    public void test2() throws Exception{
        String name = "jxs";
        String number = "12345";
        String age = "23";

        Person p = new Person();
        BeanUtils.setProperty(p,"name",name);
        BeanUtils.setProperty(p,"number",number);
        BeanUtils.setProperty(p,"age",age);

        System.out.println(p.getName());
        System.out.println(p.getNumber());
        System.out.println(p.getAge());
    }

    //自行实现接口,转换Date类型
    @Test
    public void test3() throws Exception{
        String name = "jxs";
        String number = "12345";
        String age = "23";
        String birthday = "1994-01-30";

        //为使得BeanUtils.setproperty支持将日期赋到bean的birthday属性上,给beanUtils注册一个日期转换器
        //convert:Converter to be registered
        //Date.class:Destination class for conversions performed by this Converter
        ConvertUtils.register(new Converter() {
            @Override
            public <T> T convert(Class<T> 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 dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    return (T) dateFormat.parse(str);
                }
                catch (ParseException e){
                    throw new RuntimeException(e);//异常链不能断
                }

            }
        }, Date.class);

        Person p = new Person();
        BeanUtils.setProperty(p,"name",name);
        BeanUtils.setProperty(p,"number",number);
        BeanUtils.setProperty(p,"age",age);
        BeanUtils.setProperty(p,"birthday",birthday);

        System.out.println(p.getName());
        System.out.println(p.getNumber());
        System.out.println(p.getAge());
        System.out.println(p.getBirthday());
    }

    //使用DateLocaleConverter()转换Date类型,健壮性不如test3()
    @Test
    public void test4() throws Exception{
        String name = "jxs";
        String number = "12345";
        String age = "23";
        String birthday = "1994-01-30";

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

        Person p = new Person();
        BeanUtils.setProperty(p,"name",name);
        BeanUtils.setProperty(p,"number",number);
        BeanUtils.setProperty(p,"age",age);
        BeanUtils.setProperty(p,"birthday",birthday);

        System.out.println(p.getName());
        System.out.println(p.getNumber());
        System.out.println(p.getAge());
        System.out.println(p.getBirthday());
    }

    //使用BeanUtils.populate(),将map中的数据填充至bean中
    @Test
    public void test5() throws Exception{
        Map map = new HashMap();
        map.put("name","jxs");
        map.put("number","12345");
        map.put("age","23");
        map.put("birthday","1994-01-30");

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

        Person p = new Person();
        BeanUtils.populate(p,map);

        System.out.println(p.getName());
        System.out.println(p.getNumber());
        System.out.println(p.getAge());
        System.out.println(p.getBirthday());
    }
}

test1()函数测试结果:

jxs

Process finished with exit code 0

test2()函数测试结果:

jxs
12345
23

Process finished with exit code 0

test3();test4();test5();测试出结果相同,如下:

jxs
12345
23
Sun Jan 30 00:00:00 CST 1994

Process finished with exit code 0


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值