Java ModelMapper如何在条件满足时使用
我在严格模式下使用ModelMapperJava ModelMapper如何在条件满足时使用,java,modelmapper,Java,Modelmapper,我在严格模式下使用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
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;
}
地图(来源:学生到目的地:学生到)
为了在地址为空时转义映射,我设置了以下条件
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);
}
};
条件conditionAddressIsNull=新条件(){
应用公共布尔值(映射上下文){
返回context.getSource().getAddress()==null;
}
};
PropertyMap PropertyMapToStudentDTO=新属性映射(){
受保护的void configure(){
when(conditionAddressIsNull).map(source).setStreet(null);
when(conditionAddressIsNull).map(source).setCity(null);
}
};
问题是:即使地址不为null,我也会获得等于null的street和city
如何使用严格映射来修复此问题您应该在(**conditionAddressIsNotNull**)时使用
而不是在(**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());
}
};
PropertyMap PropertyMapToStudentDTO=new PropertyMap(){
受保护的void configure(){
when(conditionAddressIsNotNull).map().setStreet(source.getAddress().geStreet());
when(conditionAddressIsNotNull).map().setCity(source.getAddress().getCity());
}
};