当映射信息由第二个映射函数明确提供给 Mapstruct 时,为什么这不会自动生成映射代码?
Flat Composed
---- --------
- String a - String a
- Sub
- String b - String b
- String c - String c
.
@Mapper(uses = SubToFlatMapper.class)
public interface ComposedToFlatMapper {
Flat map(Composed c); // Unmapped target properties: "b, c".
// Not OK because Sub mapping is defined in
// SubToFlatMapper and is used here :(
}
@Mapper
public interface SubToFlatMapper {
Flat map(Sub s); // Unmapped target properties: "a".
// (OK because 'a' is not in Sub)
}
为清楚起见的用法:
Composed composed = new Composed();
Sub sub = new Sub();
composed.setA("A");
composed.setSub(sub);
sub.setB("B");
sub.setC("C");
Flat flat = ComposedToFlatMapper.INSTANCE.map(composed);
// flat.getA() is "A", OK!
// flat.getB() is null, unexpected, should be "B"
// flat.getC() also null, should be "C".
"B"
并且"C"
没有映射到,flat
因为生成代码没有为此创建方法。
我以为我已经向生成器提供了所需的映射信息。ComposedToFlatMapperImpl
当它看到该Flat map(Sub s)
方法时,它应该在内部生成。
编辑:
我之前没有提到这一点,但在最初的问题中,我也可以使用update
更灵活的 -methods。
现在,假设有一个新Sub2
类型。:
@Mapper(uses = {SubToFlatMapper.class, Sub2ToFlatMapper.class})
public interface ComposedToFlatMapper {
Flat map(Composed c);
void update(Composed source, @MappingTarget Flat target);
}
@Mapper
public interface SubToFlatMapper {
Flat map(Sub s);
void update(Sub source, @MappingTarget Flat target);
}
@Mapper
public interface Sub2ToFlatMapper {
Flat map(Sub2 s);
void update(Sub2 source, @MappingTarget Flat target);
}
.
Flat Composed
---- --------
- String a - String a
- Sub
- String b - String b
- String c - String c
- Sub2
- String d
- String e
由于Flat
没有Sub2
参考,请忽略它。无需生成映射代码。