提问者:小点点

将参数传递给自定义映射函数


使用MapStruct,并希望将常量/参数传递给自定义映射器

我的目的地具有 Map 类型的字段

   @Mappings({

          @Mapping(source="source.someField", target="restrictions", constant="someKey"),

          @Mapping(source="source.startDate", target="restrictions", constant="someKey"),

          @Mapping(source="source.EndDate", target="restrictions", constant="someKey")

   })

   public abstract void restrictToClassA(SomeDataEntity source, @MappingTarget ClassA destination);

共1个答案

匿名用户

常量是目标的替代方法。不能在同一批注@Mapping中同时指定两者。但是,从你的故事中,我发现你可以控制你的“A类”目的地。因此,您可以将映射分组为四胞胎。所以:

Public Quadrupel{

   String prop1;
   String prop2;
   String prop3;
    //Getters/setters

}

ClassA{

   Quadrupel key1;
   Quadrupel key2;
    //Etc
    //Getters/setters

}

进入A类。然后,属性将沿着键名的行。您的自定义映射器可以拾取这些数据并将其映射到目标地图中。尽管如此,该键也会有点可疑,因为该属性将指示相同的值