JavaModelMapper Java对象活动映射

public class OrganTo {
    /***
     * 机构名称
     */
    String organName;
    /***
     * 机构Code
     */
    String organCode;
}

public class Organization {
    String name;
    String code;
}    

@Override
public List<OrganTo> getOrganList() {
    List<Organization> list = organizationDao.getOrganList();
    List<OrganTo> organList = new ArrayList<OrganTo>();
    ModelMapper modelMapper = new ModelMapper();
    modelMapper.addMappings(organMap);
    for (Organization organ : list) {
        OrganTo to = modelMapper.map(organ, OrganTo.class);
        organList.add(to);
    }
    return organList;
}

PropertyMap<Organization,OrganTo> organMap = new PropertyMap<Organization, OrganTo>() {
      protected void configure() {
          map().setOrganCode(source.getCode());
          map().setOrganName(source.getName());
      }
};

便宜处理po 到 to的数据copy,因为是小品种,对品质没有影响,所以没有做测试

Java,原文 http://modelmapper.org/getting-started/

 

相关文章