ModelMapper如何使用条件

恢弘志士之气,不宜妄自菲薄。这篇文章主要讲述ModelMapper如何使用条件相关的知识,希望能为你提供帮助。
我在Strict模式下使用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; }

地图(来源:学生到目的地: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); } };

问题是:即使地址不为空,我获得街道和城市等于null如何使用STRICT映射来修复它
答案你应该when(**conditionAddressIsNotNull**)而不是when(**conditionAddressIsNull**)
when(condition).map()的意思是:当condition = true时,我们映射; 否则,我们跳过。
【ModelMapper如何使用条件】我建议你可以试试
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()); } };


    推荐阅读