第一步
创建一个用来演示的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}