gpt4 book ai didi

java - ModelMapper如何使用when条件

转载 作者:行者123 更新时间:2023-11-30 06:07:40 25 4
gpt4 key购买 nike

我在严格模式下使用 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;
}

map (来源:学生到目的地:StudentDto)

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

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);
}
};

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

最佳答案

您应该使用 when(**conditionAddressIsNotNull**) 而不是 when(**conditionAddressIsNull**)

when(condition).map() 意思是:当condition=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());
}
};

关于java - ModelMapper如何使用when条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50953911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com