如何在Mapstruct中设置Mapping来获取Child的DTO中Parent的字段?

问题描述 投票:0回答:1
public class Parent {
    String parentField;
}

public class Child extends Parent{
}

public class ChildDTO{
String parentField;
}

@Mapper (componentModel="spring")
public interface ChildMapper {
   
    ChildDTO toDTO(Child child);
}

如何在Mapstruct中设置Mapping来获取Child的DTO中Parent的字段

我试过这个:

@Mapper(componentModel = "spring") 公共接口 ChildMapper {

@Mapping(source = "parentField", target = "parentField")
ChildDTO toDTO(Child child);
}

我也尝试过

@Mapper(componentModel = "spring")
public interface ChildMapper {
   
    @Mapping(source = "parentField", target = "parentField", qualifiedByName = "mapParentField")
    ChildDTO toDTO(Child child);

    @Named("mapParentField")
    default String mapParentField(Parent parent) {
        return parent.getParentField();
    }
}
java mapstruct
1个回答
0
投票

您需要添加 Getter 和 Setter。 为了简单起见,我使用 Lombok

家长:

@Data
public class Parent {

     private String parentField;
     
}

孩子:

@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class Child extends Parent {

}

儿童DTO:

@Data
public class ChildDTO {

    private  String parentField;
     
}

儿童映射器:

@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface ChildMapper {
   
    ChildMapper INSTANCE = Mappers.getMapper(ChildMapper.class);
    
    ChildDTO toDTO(Child child);
    
}

生成的ChildMapperImpl:

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2023-10-04T22:29:28+0200",
    comments = "version: 1.5.5.Final, compiler: Eclipse JDT (IDE) 1.4.300.v20221108-0856, environment: Java 17.0.5 (Eclipse Adoptium)"
)
@Component
public class ChildMapperImpl implements ChildMapper {

    @Override
    public ChildDTO toDTO(Child child) {
        if ( child == null ) {
            return null;
        }

        ChildDTO childDTO = new ChildDTO();

        childDTO.setParentField( child.getParentField() );

        return childDTO;
    }
}

如您所见,生成的映射器正在正确地将字段从父DTO映射到子DTO。

无需添加@Mapping,因为它们具有相同的字段名称。

它在相反的方向上也能完美工作,所以如果我将 toEntity 方法添加到 ChildMapper 中:

@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface ChildMapper {
   
    ChildMapper INSTANCE = Mappers.getMapper(ChildMapper.class);
    
    ChildDTO toDTO(Child child);
    
    Child toEntity(ChildDTO dto);
    
}

ChildMapperImpl 看起来像这样:

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2023-10-04T22:36:40+0200",
    comments = "version: 1.5.5.Final, compiler: Eclipse JDT (IDE) 1.4.300.v20221108-0856, environment: Java 17.0.5 (Eclipse Adoptium)"
)
@Component
public class ChildMapperImpl implements ChildMapper {

    @Override
    public ChildDTO toDTO(Child child) {
        if ( child == null ) {
            return null;
        }

        ChildDTO childDTO = new ChildDTO();

        childDTO.setParentField( child.getParentField() );

        return childDTO;
    }

    @Override
    public Child toEntity(ChildDTO dto) {
        if ( dto == null ) {
            return null;
        }

        Child child = new Child();

        child.setParentField( dto.getParentField() );

        return child;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.