/*
*
* 为了保证原bean中的数据不为空,需要自己设置转换器。
* 当执行BeanUtils.copyProperties(dest, src);时会首先去调用转换器(Converter接口的实现方法convert),然后再执行转换.
*/
public static void copyBean(Object src, Object dest) {
try{
//注册日期转换器 frombean-->birthday="" user-birhday=null
ConvertUtils.register(new Converter(){
public Object convert(Class type, Object value) { //198a
if(value==null){
return null;
}
String s = (String)value;
if(s.trim().equals("")){
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(s);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}, Date.class);
//实现bean的拷呗
BeanUtils.copyProperties(dest, src);
}catch (Exception e) {
throw new RuntimeException(e);
}
}
用BeanUtils类实现bean的拷贝
最新推荐文章于 2023-06-08 15:23:52 发布