是否有一种方法可以防止mapstruct重写我在mapper类中提供的实现的特定方法?
我在映射器类entityDTOToVehicle中提供了一个方法EntityMapper,在为该类生成映射时,mapstruct忽略了提供的实现,并用自己的实现覆盖了我的实现。
final方法,它不工作@Mapping中尝试过使用qualifiedByName,它不工作我的地图类如下所示:
public abstract class EntityMapper {
public static final EntityMapper INSTANCE = Mappers.getMapper(EntityMapper.class);
protected final Vehicle EntityDTOToVehicle(EntityDTO EntityDTO) {
Vehicle vehicle = new Vehicle();
//My Implementation Here
return vehicle;
}
@Mapping(target = "vehicle.property1", source = "vehicleProperty1")
@Mapping(target = "vehicle.property2", source = "vehicleProperty2")
public abstract Entity map(EntityDTO dto);
}然后Mapstruct生成如下所示的实现:
@Component
public class EntityMapperImpl extends EntityMapper {
@Override
public Entity map(EntityDTO dto) {
if ( dto == null ) {
return null;
}
Entity entity = new Entity();
.
.
.
entity.setTransport( entityDTOToVehicle( dto ) );
return entity;
}
/**
* This is the method I'd like to prevent mapstruct from overriding */
protected Vehicle entityDTOToVehicle(EntityDTO entityDTO) {
Vehicle vehicle = new Vehicle();
//Mapstruct's Implementation
return vehicle;
}
}发布于 2020-07-03 11:09:09
您必须在实现中使用@Named。
@Named(value = "mappingName")
protected Vehicle EntityDTOToVehicle(EntityDTO EntityDTO) {
Vehicle vehicle = new Vehicle();
//My Implementation Here
return vehicle;
}然后将其附加到抽象映射程序:
@Mappings(value ={
@Mapping(target = "vehicle.property1", source = "vehicleProperty1")
@Mapping(target = "vehicle.property2", source = "vehicleProperty2")
@Mapping(source = "fieldWithVehiclePropChangeItToYours", target = "transport", qualifiedByName = "mappingName"),
}
public abstract Entity map(EntityDTO dto);https://stackoverflow.com/questions/62713699
复制相似问题