首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >防止Mapstruct重写方法

防止Mapstruct重写方法
EN

Stack Overflow用户
提问于 2020-07-03 10:40:24
回答 1查看 1.1K关注 0票数 0

是否有一种方法可以防止mapstruct重写我在mapper类中提供的实现的特定方法?

我在映射器类entityDTOToVehicle中提供了一个方法EntityMapper,在为该类生成映射时,mapstruct忽略了提供的实现,并用自己的实现覆盖了我的实现。

  • 我试过做final方法,它不工作
  • ,我在@Mapping中尝试过使用qualifiedByName,它不工作

我的地图类如下所示:

代码语言:javascript
复制
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生成如下所示的实现:

代码语言:javascript
复制
@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;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-07-03 11:09:09

您必须在实现中使用@Named

代码语言:javascript
复制
@Named(value = "mappingName")
protected Vehicle EntityDTOToVehicle(EntityDTO EntityDTO) {
    Vehicle vehicle = new Vehicle();

    //My Implementation Here

    return vehicle;
}

然后将其附加到抽象映射程序:

代码语言:javascript
复制
@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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62713699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档