import java.util.Date;
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;
}
}
@Test
public void springBeanUtilsTest(){
People oldPeople = new People("oldName",100,new Date());
People newPeople = new People();
//BeanUtils.copyProperties(Object source,Object target);
BeanUtils.copyProperties(oldPeople, newPeople);
System.out.println(oldPeople);
System.out.println(newPeople);
}
People [name=oldName, age=100, birth=Wed Jul 19 18:46:13 CST 2017] People [name=oldName, age=100, birth=Wed Jul 19 18:46:13 CST 2017]
@Test
public void springBeanUtilsTest(){
People oldPeople = new People(null,100,new Date());
People newPeople = new People("newName",20,null);
//BeanUtils.copyProperties(Object source,Object target);
BeanUtils.copyProperties(oldPeople, newPeople);
System.out.println(oldPeople);
System.out.println(newPeople);
}
People [name=null, age=100, birth=Wed Jul 19 19:04:48 CST 2017] People [name=null, age=100, birth=Wed Jul 19 19:04:48 CST 2017]
@Test
public void springBeanUtilsTest(){
People oldPeople = new People("oldName",100,null);
People newPeople = new People("newName",20,new Date());
//BeanUtils.copyProperties(Object source,Object target);
BeanUtils.copyProperties(oldPeople, newPeople);
System.out.println(oldPeople);
System.out.println(newPeople);
}
People [name=oldName, age=100, birth=null] People [name=oldName, age=100, birth=null]
BeanUtils.copyProperties(Object source,Object target, String... ignoreProperties);
/**
*
* @Title: getNullPropertyNames
* @Description: 获取一个对象中属性值为null的属性名字符串数组
* @param source
* @return
*/
public static String[] getNullPropertyNames (Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set<String> emptyNames = new HashSet<String>();
for(java.beans.PropertyDescriptor pd : pds) {
Object srcValue = src.getPropertyValue(pd.getName());
if (srcValue == null) emptyNames.add(pd.getName());
}
String[] result = new String[emptyNames.size()];
return emptyNames.toArray(result);
}
@Test
public void copyBeanNotNull(){
People oldPeople = new People(null, 100, null);
People newPeople = new People("newName", 20, new Date());
//BeanUtils.copyProperties(Object source,Object target, String... ignoreProperties);
BeanUtils.copyProperties(oldPeople, newPeople, getNullPropertyNames(oldPeople));
System.out.println(oldPeople);
System.out.println(newPeople);
for(String key : getNullPropertyNames(oldPeople)){
System.out.println(key);
}
}
People [name=null, age=100, birth=null] People [name=newName, age=100, birth=Wed Jul 19 23:31:05 CST 2017] name birth
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有