当前位置: 首页 > 知识库问答 >
问题:

Mapstruct继承并调用“this”

阳兴朝
2023-03-14

我有一个非规范化的数据库和一个包含多个类的领域模型。例如

class BMW {
  EngineConfiguration engineConfiguration;
  ModelMetadata modelMetadata;
  BodyConfiguration bodyConfiguration;
}

class CarsEntity{
  String modelName;
  Integer year...
  //etc. all fields, in same class
}

我想为EngineConfiguration、ModelMetadata和BodyConfiguration编写MapStruct映射程序,然后将它们合并到一个映射程序中。实例

@Mapper(componentModel = "spring")
class EngineConfigurationMapper {
  
  @Mapping(... specific fields mapping goes here)
  EngineConfiguration mapToDomain(CarsEntity carsEntity);
}

@Mapper(componentModel = "spring", uses = {EngineConfigurationMapper.class , ...})
class BMWMapper {
  BMW mapToDomain(CarsEntity carsEntity);
}

问题是我的映射器默认情况下不工作,BMWMapper没有对其他映射器的调用,我尝试了调用this的表达式,如expression="(engineConfigurationMapper.mapToDomain(this))"target="工程配置),但映射器不包含在实现中。我如何实现这一点?附言:MapperConfig也没用。

共有1个答案

东门航
2023-03-14

我想当你说这个时,你是指源实体,即carsEntity

在< code>Mapping#source中,您可以使用源参数名称,这意味着您可以执行以下操作:

@Mapper(componentModel = "spring", uses = {EngineConfigurationMapper.class , ...})
class BMWMapper {

  @Mapping( target = "engineConfiguration", source = "carsEntity")
  @Mapping( target = "modelMetadata", source = "carsEntity")
  @Mapping( target = "bodyConfiguration", source = "carsEntity")
  BMW mapToDomain(CarsEntity carsEntity);
}

 类似资料:
  • 我有一个BaseEntity,当我像这样制作mapper时,它有一个名为Customer的子级: Mapstruct不会自动映射BaseEntity字段。你能告诉我怎么做吗?

  • 我有一个扩展了B类的a类。 A是这样定义的,它也覆盖了B的方法: B是这样定义的: 因此,如果我初始化A的一个对象,构造函数将调用调用方法doSomething()的超类之一。但哪一个会被处决?B的实现还是A中被重写的实现?

  • 问题内容: 假设我有以下两个课程 如果我启动一个beta类型的新对象,如何执行在alpha类而不是beta中找到的逻辑?我可以使用<-我想知道是否可行。 Eclipse IDE中的自动键入功能使我可以选择从class 或class中进行选择。 问题答案: 你可以做: 注意,这是对父级的引用,但是super()是它的构造函数。

  • FAQs in section [24]: [24.1] 如何表示“私有继承”? [24.2] 私有继承和组合(composition)有什么类似? [24.3] 我应该选谁:组合还是私有继承? [24.4] 从私有继承类到父类需要指针类型转换吗? [24.5] 保护继承和私有继承的关系是什么? [24.6] 私有继承和保护继承的访问规则是什么? 24.1 如何表示“私有继承”? 用 : priv

  • 问题内容: 假设Java具有以下层次结构类: 这是C#中相同代码的(盲)重复: 当我执行Java代码时,我得到了C#返回的信息。 对我来说,C#的结果更有意义,因为引用B调用了它自己的方法。 Java设计者决定打印而不是打印的逻辑是什么?我的意思是,为什么引用B在C中使用覆盖方法?这种方法的优势是什么? 如何更改Java代码以像C#一样打印出来?我的意思是,我怎么教Java调用它使用的完全引用的方

  • 本文向大家介绍JS继承之借用构造函数继承和组合继承,包括了JS继承之借用构造函数继承和组合继承的使用技巧和注意事项,需要的朋友参考一下 借用构造函数继承  在解决原型中包含引用类型值所带来问题的过程中,开发人员开始使用一种叫做借用构造函数(constructor stealing)的技术(有时候也叫做伪造对象或经典继承)。这种技术的基本思想相当简单,即在子类型构造函数的内部调用超类型构造函数。