使用 MapperFacade 实现 List 映射拷贝到 List<Pod>
假设有如下两个类:
public class Source {
private String name;
private int age;
// 省略getter和setter方法
}
public class Pod {
private String podName;
private int podAge;
// 省略getter和setter方法
}
我们需要将一个List<Source>映射拷贝到一个List<Pod>中,可以使用MapperFacade来完成:
List<Source> sourceList = new ArrayList<>();
sourceList.add(new Source('Tom', 18));
sourceList.add(new Source('Jerry', 20));
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapperFactory.classMap(Source.class, Pod.class)
.field('name', 'podName')
.field('age', 'podAge')
.byDefault()
.register();
MapperFacade mapperFacade = mapperFactory.getMapperFacade();
List<Pod> podList = mapperFacade.mapAsList(sourceList, Pod.class);
首先,我们创建了一个sourceList,里面包含两个Source对象。
接下来,我们创建了一个MapperFactory,并定义了一个classMap,用于将Source类映射拷贝到Pod类中。这里使用了field方法来指定属性映射关系,例如'name'映射到'podName'。
最后,我们通过MapperFacade的mapAsList方法将sourceList映射拷贝到了podList中。
原文地址: http://www.cveoy.top/t/topic/oBHb 著作权归作者所有。请勿转载和采集!