【问题标题】:ModelMapper How to map DTO to Domain Model without overriding attributsModelMapper 如何在不覆盖属性的情况下将 DTO 映射到域模型
【发布时间】:2018-07-17 18:08:56
【问题描述】:

我的问题很简单,但直到现在我还没有找到该怎么做? 给定以下课程

public class DTOStudent{
private id;
private name;
private address;
}


public class Student{
private id;
private name;
private address;
}

使用ModelMapper 我必须将源:DTOStudent 映射到目标:Student 但在相同的情况下无需修改 Student 类中的 address 属性。

我正在使用modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);

【问题讨论】:

    标签: spring dto modelmapper


    【解决方案1】:

    请尝试skipping properties

    在 Java 6/7 中

    modelMapper.addMappings(new PropertyMap<DTOStudent, Student>() {
        skip().setAddress(null);
    });
    

    在 Java 8 中

    modelMapper.typeMap(DTOStudent.class, Student.class)
        .addMappings(mapper -> mapper.skip(Student::setAddress));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 2011-01-05
      • 1970-01-01
      相关资源
      最近更新 更多