public class MySim {
private Long id ;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
通过BeanUtils.copyProperties做copy的时候,Long ,Integer等字段的默认值设置为null
import java.lang.reflect.InvocationTargetException;
public class TestBeanUtils {
public static void main(String[] args){
MySim m1= new MySim();
MySim m2= new MySim();
try {
org.apache.commons.beanutils.BeanUtils.copyProperties(m1,m2);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
System.out.println("m2's id is null >>> "+m2.getId());
System.out.println("but,copy to m1, m1's id is 0 ??? "+m1.getId());
}
}
结果如下:
m2's id is null >>> null
but,copy to m1, m1's id is 0 ??? 0
有人说可以使用PropertyUtils.copyProperties,但是data类型的可能有问题
但是看到很多人在很久之前就遇到过了。看来是自己孤陋寡闻了~