import org.springframework.beans.BeanUtils;
BeanUtils.copyProperties(source,target);
import org.apache.commons.beanutils.BeanUtils;
BeanUtils.copyProperties(target,source);
springframework中如果Date类型不赋值 默认为null 而commons的则会报错
springframework中如果不是包装类型会报错 而common中如果声明为int float类型不会报错 而且如果没有赋值 还会默认赋予初值
以下测试使用的springframework的类:
@Data
public class Order {
private Integer id;
private String name;
private Date found_time;
public Order() {
}
public Order(Integer id, String name, Date found_time) {
this.id = id;
this.name = name;
this.found_time = found_time;
}
}
@Data
public class OrderUpgrade {
private Integer id;
private String name;
private Date found_time;
private String remark;
}
public class test23 {
public static void main(String[] args) {
// Order order = new Order(123,"Ljs",new Date());
Order order = new Order();
OrderUpgrade orderUpgrade = new OrderUpgrade();
try {
BeanUtils.copyProperties(order,orderUpgrade);
System.out.println(orderUpgrade.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}