如果這兩個類的要轉化的屬性其屬性名不一樣的話,那只能用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;