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;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM