MapStruct:来自不同实体的字段映射到同一实体的不同字段
MapStruct: Fields from Different Entities mapping to Same Entity Different fields
我有 Class A、B 和 C。Class A 有三个变量,它根据 Class B 和 C 的不同字段填充。当我使用 mapper 来填充对象仅保留最后使用的映射器值。我怎样才能确保所有值都被填充。
我正在使用 MapStruct 映射器(版本 1.2.0)和 Spring 引导版本 2.0.3
Sample code below:
Class A{
String a;
String b;
String c;
}
Class B{
String b1;
}
Class C{
String c1;
}
public interface AtoCmapper{
@Mappings({
@Mapping(target="c", source="source.c1")
})
A CtoA(C source);
@Mappings({
@Mapping(target="c1", source="source.c")
})
C AtoC(A source);
}
public interface AtoBmapper{
@Mappings({
@Mapping(target="b", source="source.b1")
})
A BtoA(B source);
@Mappings({
@Mapping(target="b1", source="source.b")
})
B AtoB(A source);
}
Class DAO{
@Autowired
AtoBmapper aToBMapper;
@Autowired
AtoCmapper aToCMapper;
public void testMapping(){
A aObject = new A();
aObject = aToBMapper(bObject); // Assume bObject is object to Type B and it is initialized properly
aObject = aToCMapper(cObject); // Assume cObject is object to Type B and it is initialized properly
}
//Now when I call testMapping() resulting A object only has value set for variable c, value of b is lost.
How can I make sure that Values from B and C are set properly to Class A
}
MapStruct 可以update existing instances!
为此,通过具有第二个 @MappingTarget
类型 A
注释参数的变体引入新的映射方法(或替换旧方法)(在这种情况下,可以替换 return输入 void
):
// In AtoBmapper
@Mappings(...)
void updateAFromB(B source, @MappingTarget A target);
// In AtoCmapper
@Mappings(...)
void updateAFromC(C source, @MappingTarget A target);
那你可以这样写:
A aObject = new A();
aToBMapper.updateAFromB(bObject, aObject);
aToCMapper.updateAFromC(cObject, aObject);
// now aObject should contain the cumulated data from b and c
我有 Class A、B 和 C。Class A 有三个变量,它根据 Class B 和 C 的不同字段填充。当我使用 mapper 来填充对象仅保留最后使用的映射器值。我怎样才能确保所有值都被填充。 我正在使用 MapStruct 映射器(版本 1.2.0)和 Spring 引导版本 2.0.3
Sample code below:
Class A{
String a;
String b;
String c;
}
Class B{
String b1;
}
Class C{
String c1;
}
public interface AtoCmapper{
@Mappings({
@Mapping(target="c", source="source.c1")
})
A CtoA(C source);
@Mappings({
@Mapping(target="c1", source="source.c")
})
C AtoC(A source);
}
public interface AtoBmapper{
@Mappings({
@Mapping(target="b", source="source.b1")
})
A BtoA(B source);
@Mappings({
@Mapping(target="b1", source="source.b")
})
B AtoB(A source);
}
Class DAO{
@Autowired
AtoBmapper aToBMapper;
@Autowired
AtoCmapper aToCMapper;
public void testMapping(){
A aObject = new A();
aObject = aToBMapper(bObject); // Assume bObject is object to Type B and it is initialized properly
aObject = aToCMapper(cObject); // Assume cObject is object to Type B and it is initialized properly
}
//Now when I call testMapping() resulting A object only has value set for variable c, value of b is lost.
How can I make sure that Values from B and C are set properly to Class A
}
MapStruct 可以update existing instances!
为此,通过具有第二个 @MappingTarget
类型 A
注释参数的变体引入新的映射方法(或替换旧方法)(在这种情况下,可以替换 return输入 void
):
// In AtoBmapper
@Mappings(...)
void updateAFromB(B source, @MappingTarget A target);
// In AtoCmapper
@Mappings(...)
void updateAFromC(C source, @MappingTarget A target);
那你可以这样写:
A aObject = new A();
aToBMapper.updateAFromB(bObject, aObject);
aToCMapper.updateAFromC(cObject, aObject);
// now aObject should contain the cumulated data from b and c