java DTO 转 POJO

如果这两个类的要转化的属性其属性名不一样的话,那只能用get和set方法赋值

如果你的两个类要转化的属性名都一样,那可以用org.springframework.beans.BeanUtils这个类来转化,转化方法BeanUtils.copyProperties(源对象, 目标对象),不过这个类要基于spring的jar包,只能在spring框架下用。

 

示例代码: 

List<Device> deviceList = new ArrayList<Device>();
List<DeviceDto> deviceDtoList = new ArrayList<DeviceDto>();
 
deviceDtoList= deviceDao.listByAuthority(null,null,devIds,userID);
for(int i=0;i<deviceDtoList.size();i++)
{
Device dev = new Device();
BeanUtils.copyProperties(deviceDtoList.get(i),dev);  // 将dto转成pojo :dev里
deviceList.add(dev);
}

return deviceList;

 

posted on 2017-12-20 10:22  lui  阅读(3203)  评论(0编辑  收藏  举报