我正在使用 mapstruct,我想知道是否有任何方法可以为某些目标属性设置空值。例如,
public class MySource {
private String prop1;
private String prop2;
public MySource() {
// Initialization.
}
// Getters - Setters.
}
public class MySourceDto {
private String prop1;
private String prop2;
public MySourceDto() {
// Initialization.
}
// Getters - Setters.
}
@Mapper
public interface MySourceMapper {
@Mappings({
@Mapping(target = "prop1", propertyToSetNull = null)})
public MySourceDto toView(MySource mySource);
}
我会喜欢上面的源代码、dto 和映射器来生成下面的源代码,
@Component
public class MySourceMapperImpl implements MySourceMapper {
@Override
public MySourceDto toView(MySource mySource) {
if ( mySource == null ) {
return null;
}
MySourceDto mySourceDto = new MySourceDto();
mySourceDto.setProp1( mySource.getId() );
mySourceDto.setProp2( null );
return mySourceDto;
}
}