如果这两个类的要转化的属性其属性名不一样的话,那只能用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;

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-18
  • 2022-12-23
  • 2021-11-17
猜你喜欢
  • 2022-12-23
  • 2022-02-25
  • 2022-01-14
  • 2022-12-23
  • 2021-08-27
  • 2021-09-19
相关资源
相似解决方案