当前位置: 首页 > 知识库问答 >
问题:

ModelMapper如何使用when条件

康弘义
2023-03-14

我在严格模式下使用ModelMapper

public class Student {
    private String fullName;
    private Address address ;
}

public class StudentDto {
    private String fullName;
    private String street;
    private String city;
}

public class Address {
    private String street;
    private String city;
}

地图(来源:学生到目的地:学生Dto)

为了在地址为空时转义映射,我设置了以下条件

Condition<Student, StudentDto> conditionAddressIsNull = new Condition<Student, StudentDto>() {
    public boolean applies(MappingContext<Student, StudentDto> context) {
        return context.getSource().getAddress() == null;
    }
};

PropertyMap<Student, StudentDto> propertryMapToStudentDto = new PropertyMap<Student, StudentDto>() {
    protected void configure() {
        when(conditionAddressIsNull).map(source).setStreet(null);
        when(conditionAddressIsNull).map(source).setCity(null);
    }
};

问题是:即使地址不为null,我也要使街道和城市等于null。如何使用严格的映射来解决这个问题

共有1个答案

谭越
2023-03-14

当(**conditionAddressIsNotNull**)时,您应该,而不是当(**conditionAddressIsNull**)时。

当(条件). map()表示:当条件=true时,我们映射;否则,我们跳过。

我建议你可以试试

PropertyMap<Student, StudentDto> propertryMapToStudentDto = new PropertyMap<Student, StudentDto>() {
    protected void configure() {
        when(conditionAddressIsNotNull).map().setStreet(source.getAddress().geStreet());
        when(conditionAddressIsNotNull).map().setCity(source.getAddress().getCity());
    }
};
 类似资料:
  • 我读过其他的解决方案,其中提到创建一个模拟类/对象,我相信我已经用“booksearch mockBookSearch=mockito.mock(booksearch.class);” 我错过了什么?

  • 我正在尝试使用ModelMapper映射对象树。 我创建了一个例子来说明我的问题: 类包含多个属性 类包含类型为Sub的对象和(至少)另一个属性 类目标包含一个简单的属性列表 源属性和目标属性的类型不同 代码: 我正在寻找一种配置单个ModelMapper实例的方法,以便满足以下约束: modelMapper能够将Sub类型的对象转换为目标对象 不幸的是,行<代码>映射(source.sub,de

  • 问题内容: 这是一个演示查询,请注意,这非常简单,仅在base_price为0的地方获取,并且仍然选择条件3: 是 在数据库上运行此命令时,我得到: 3 0 3 0 3 0 3 0 3 0 问题答案: 删除后立即: 有两种不同的形式,如手册中所述。在这里,您要使用第二种形式,因为您正在使用 搜索条件 。

  • MissingMethodInvocationException:when()需要一个必须是“mock上的方法调用”的参数。例如:when(mock.getarticles()).ThenReturn(articles); 此外,出现此错误的原因可能是:1。您使用final/private/equals()/hashCode()方法中的任一个作为存根。这些方法不能被截取/验证。2.内部when()

  • 问题内容: 给出以下Mockito语句: 考虑到嘲笑方法将返回值传递给when(),Mockito如何为该嘲笑创建代理?我想这会使用一些CGLib的东西,但是有兴趣知道这在技术上是如何完成的。 问题答案: 简短的答案是,在您的示例中,的结果mock.method()将是类型合适的空值;Mockito通过代理,方法拦截和类的共享实例使用间接方式,MockingProgress以确定对模拟方法的调用是

  • 我正在尝试使用ModelMapper将实体映射到DTO。当@JoinCollie未加载(延迟加载)时,问题就出现了。ModelMapper尝试访问延迟加载实体的属性,然后抛出LazyLaunalizationException。 我已经有了解决这个问题的策略,但我找不到一个ModelMapper功能来满足我的需要。 我需要做的是:对于每个未加载的实体,我将使用工厂创建一个新的目标对象。如果加载了对