如何在 MapStruct 中禁用源映射中的字段?
How can I disable a field in source mapping in MapStruct?
例如,我有一个映射 class 和一个未在映射 class 中显示的字段。
一个class,哪个我要映射:
@Entity
@Table(name = "t_connection")
@Getter @Setter
@EqualsAndHashCode
public class ConnectionEntity {
@NotNull
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
protected UUID id;
...
}
一个class,其中我要贴图:
@ApiModel
@Getter
@Setter
@NoArgsConstructor
public class ConnectionDto {
@ApiModelProperty
private LocalDateTime createAt;
...
// Other fields without id field
}
我的映射器看起来像这样:
@Mapper(componentModel = "spring",
unmappedTargetPolicy = ReportingPolicy.ERROR,
unmappedSourcePolicy = ReportingPolicy.ERROR)
public interface CallMapper {
@IterableMapping(qualifiedByName = "map")
List<ConnectionDto> map(List<ConnectionEntity> connectionEntities);
ConnectionDto map(ConnectionEntity connectionEntity);
}
我想知道,当特定字段未被映射时,禁用 unmappedSourcePolicy
不是一个选项。有什么建议吗?
如果我理解你..你想控制你不想映射的源属性?
在那种情况下尝试:
@BeanMapping#ignoreUnmappedSourceProperties
所以:
@Mapper(componentModel = "spring",
unmappedTargetPolicy = ReportingPolicy.ERROR,
unmappedSourcePolicy = ReportingPolicy.ERROR)
public interface CallMapper {
@IterableMapping(qualifiedByName = "map")
List<ConnectionDto> map(List<ConnectionEntity> connectionEntities);
@BeanMapping( ignoreUnmappedSourceProperties={"id"} )
ConnectionDto map(ConnectionEntity connectionEntity);
}
您不需要指定列表映射,除非您从外部需要它。MapStruct 将为您生成一个。如果您确实需要外部列表,您可能不需要qualifier.. Generic + List 就够了
例如,我有一个映射 class 和一个未在映射 class 中显示的字段。
一个class,哪个我要映射:
@Entity
@Table(name = "t_connection")
@Getter @Setter
@EqualsAndHashCode
public class ConnectionEntity {
@NotNull
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
protected UUID id;
...
}
一个class,其中我要贴图:
@ApiModel
@Getter
@Setter
@NoArgsConstructor
public class ConnectionDto {
@ApiModelProperty
private LocalDateTime createAt;
...
// Other fields without id field
}
我的映射器看起来像这样:
@Mapper(componentModel = "spring",
unmappedTargetPolicy = ReportingPolicy.ERROR,
unmappedSourcePolicy = ReportingPolicy.ERROR)
public interface CallMapper {
@IterableMapping(qualifiedByName = "map")
List<ConnectionDto> map(List<ConnectionEntity> connectionEntities);
ConnectionDto map(ConnectionEntity connectionEntity);
}
我想知道,当特定字段未被映射时,禁用 unmappedSourcePolicy
不是一个选项。有什么建议吗?
如果我理解你..你想控制你不想映射的源属性?
在那种情况下尝试:
@BeanMapping#ignoreUnmappedSourceProperties
所以:
@Mapper(componentModel = "spring",
unmappedTargetPolicy = ReportingPolicy.ERROR,
unmappedSourcePolicy = ReportingPolicy.ERROR)
public interface CallMapper {
@IterableMapping(qualifiedByName = "map")
List<ConnectionDto> map(List<ConnectionEntity> connectionEntities);
@BeanMapping( ignoreUnmappedSourceProperties={"id"} )
ConnectionDto map(ConnectionEntity connectionEntity);
}
您不需要指定列表映射,除非您从外部需要它。MapStruct 将为您生成一个。如果您确实需要外部列表,您可能不需要qualifier.. Generic + List 就够了