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 getOrganList() {
    List list = organizationDao.getOrganList();
    List organList = new ArrayList();
    ModelMapper modelMapper = new ModelMapper();
    modelMapper.addMappings(organMap);
    for (Organization organ : list) {
        OrganTo to = modelMapper.map(organ, OrganTo.class);
        organList.add(to);
    }
    return organList;
}

PropertyMap organMap = new PropertyMap() {
      protected void configure() {
          map().setOrganCode(source.getCode());
          map().setOrganName(source.getName());
      }
};

 

你可能感兴趣的:(ModelMapper Java对象自动映射)