我正在使用 ModelMapper 将 JPA 实体映射到 DTO。我在实体上有集合 dto 是由 wsimport 从 wsdl 文件生成的,但不会生成集合的设置器
public class sampleEntity{
private String name;
private Collection<String> list;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Collection<String> getList() {
return list;
}
public void setList(Collection<String> list) {
this.list = list;
}
}
public class sampleDTO{
private String name;
private Collection<String> list;
//getters & setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Collection<String> getList() {
return list;
}
//no collection setters with jaxb!!! Use getList().add()
}
我使用一个简单的 MapperUtils 来映射实体和 dto
public class MapperUtils {
private static ModelMapper modelMapper = new ModelMapper();
static {
modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.LOOSE);
}
private MapperUtils() {
}
public static <D, T> D map(final T entity, Class<D> outClass) {
return modelMapper.map(entity, outClass);
}
public static <D, T> List<D> mapAll(final Collection<T> entityList, Class<D> outCLass) {
return entityList.stream().map(entity -> map(entity, outCLass)).collect(Collectors.toList());
}
public static <S, D> D map(final S source, D destination) {
modelMapper.map(source, destination);
return destination;
}
}
那么如果Entity.XXXX是一个Collection,如何使用ModelMapper来使用DTO.getXXXX.add()呢?
慕码人2483693
相关分类