ModelMapper Java对象自动映射

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

方便处理po 到 to的数据copy,因为是小项目,对性能没有影响,所以没有做测试

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());
      }
};

 

posted @ 2013-03-29 16:03  骨头  阅读(6351)  评论(1编辑  收藏  举报