最近的项目遇到BeanUiles.copyPropertis(),大大的简化了代码量。用hibernate从数据库中映射的实体类,与pojo对象进行转换,传统做法
Object obj = baseDAO.querySingle(hql, name,cardno);
Tobdata tobdata =(Tobdata)obj;
tobdataObject1=new TobdataObject();
tobdataObject1.setMsg(tobdata.getMsg());
tobdataObject1.setResult(tobdata.getResult());
····················
用BeanUiles.copyPropertis()只用这样
tobdataObject1=new TobdataObject();
BeanUtils.copyProperties(obj, tobdataObject1);
BeanUiles只复制相同的属性,如果两者直接存在不一样的属性,BeanUiles不会处理。网上有人说用BeanUiles代价大,具体没测试过,不知道现在有没有改进。
org.springframework.beans.BeanUtils
Static convenience methods for JavaBeans: for instantiating beans, checking
bean property types, copying bean properties, etc.
Mainly for use within the framework, but to some degree also useful for
application classes.
- Author:
- Rod Johnson
- Juergen Hoeller
- Rob Harrop