Apache BeanUtils组件使用

14 篇文章 0 订阅
4 篇文章 0 订阅

第一步
创建一个用来演示的javabean
注意:用来演示的javabean一定要具有get和set方法,不然复制属性值会失败,但不会抛异常

package com.wechat.entity;

import java.util.Date;

/**
 * 
 * @ClassName: People
 * @Description:
 * @author 
 * @date 2017年7月19日 下午4:43:25
 */
public class People {

    private String name;
    private int age;
    private Date birth;

    public People(String name, int age, Date birth) {
        super();
        this.name = name;
        this.age = age;
        this.birth = birth;
    }

    public People() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public String toString() {
        return "People [name=" + name + ", age=" + age + ", birth=" + birth + "]";
    }

    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 getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

}

第二步
测试(所有测试只与源javabean属性值有关,与目标javabean属性值无关)
当源javabean属性均有值时的目标javabean属性复制情况

@Test
public void apacheBeanCopy1() throws IllegalAccessException, InvocationTargetException{
    People oldPeople = new People("oldName",100,new Date());
    People newPeople = new People("newName",20,null);

    BeanUtils.copyProperties(newPeople, oldPeople);

    System.out.println(oldPeople);
    System.out.println(newPeople);
}

输出结果如下

People [name=oldName, age=100, birth=Wed Jul 19 17:08:13 CST 2017]
People [name=oldName, age=100, birth=Wed Jul 19 17:08:13 CST 2017]

当源javabean非Date类型的属性值为null时目标javabean属性的复制情况

@Test
public void apacheBeanCopy3() throws IllegalAccessException, InvocationTargetException{
    People oldPeople = new People(null,100,new Date());
    People newPeople = new People("newName",0,null);

    BeanUtils.copyProperties(newPeople, oldPeople);

    System.out.println(oldPeople);
    System.out.println(newPeople);
}

输出结果如下
注意:目标javabean中的非null属性值被覆盖为null了

People [name=null, age=100, birth=Wed Jul 19 17:17:22 CST 2017]
People [name=null, age=100, birth=Wed Jul 19 17:17:22 CST 2017]

当源javabean中Date类型的属性值为null时出现异常

@Test
public void apacheBeanCopy4() throws IllegalAccessException, InvocationTargetException{
    People oldPeople = new People("oldName",100,null);
    People newPeople = new People(null,0,null);

    BeanUtils.copyProperties(newPeople, oldPeople);

    System.out.println(oldPeople);
    System.out.println(newPeople);
}

抛出的异常如下

org.apache.commons.beanutils.ConversionException: No value specified for 'Date'
    at org.apache.commons.beanutils.converters.AbstractConverter.handleMissing(AbstractConverter.java:310)
    at org.apache.commons.beanutils.converters.AbstractConverter.convert(AbstractConverter.java:136)
    at org.apache.commons.beanutils.converters.ConverterFacade.convert(ConverterFacade.java:60)
    at org.apache.commons.beanutils.BeanUtilsBean.convert(BeanUtilsBean.java:1074)
    at org.apache.commons.beanutils.BeanUtilsBean.copyProperty(BeanUtilsBean.java:437)
    at org.apache.commons.beanutils.BeanUtilsBean.copyProperties(BeanUtilsBean.java:286)
    at org.apache.commons.beanutils.BeanUtils.copyProperties(BeanUtils.java:137)
    at test.BeanUtilTest.apacheBeanCopy4(BeanUtilTest.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


javabean转map

@Test
public void apacheBeanUtilsTest() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
    People people = new People("myName", 22, new Date());

    Map map = BeanUtils.describe(people);

    System.out.println(map);
}

输出结果如下

{name=myName, birth=Wed Jul 19 18:29:54 CST 2017, class=class com.ahutshop.entity.People, age=22}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值