源类和目标类都继承同一个类的情况下使用 MapStruct。

huangapple go评论96阅读模式
英文:

Source Class and Target class extending same class mapstruct

问题

抽象类 baseClass {
    String updateUser;
}

class A 扩展自 baseClass {
    String updateTime;
}

class B 扩展自 baseClass {
    String updateTime;
}

@Mapper(componentModel = "spring")
public interface FLGenericMapper {
    B mapToB(A a);
}

我有类似的代码当从 A 映射到 B 时
我发现 updateUser 没有被映射
请问是否有任何操作我应该执行以映射抽象类的 updateUser
英文:
abstract class baseClass {
 String updateUser;
}

class A extends baseClass {
 String updateTime;
}

class B extnds baseClass {
  String updateTime;
}

@Mapper(componentModel = "spring")
public interface FLGenericMapper {
  B mapToB(A a);
}

I have similar code. when mapping is done from A to B.
I see that the updateUser is not mapping.
Please some one let me know, Is there anything I should be doing to map the updateUser of abstract class.

答案1

得分: 0

以下是翻译好的内容:

我不确定问题的具体所在。您是否使用了 Lombok?是否使用了字段映射?

如果您使用了字段映射,您的字段应该是公开的(否则没有人可以访问它们)。如果您将它们设为公开,那么这对我来说是可行的。以下是我认为代码应该是的样子:

@Mapper(componentModel = "spring")
public interface MyMapper {

    B mapToB(A a);

    abstract class BaseClass {
        public String updateUser;
    }

    class A extends BaseClass {
        public String updateTime;
    }

    class B extends BaseClass {
        public String updateTime;
    }

}

结果:

@Component
public class MyMapperImpl implements MyMapper {

    @Override
    public B mapToB(A a) {
        if (a == null) {
            return null;
        }

        B b = new B();

        b.updateUser = a.updateUser;
        b.updateTime = a.updateTime;

        return b;
    }
}
英文:

I'm not sure what exactly the problem is. Do you use lombok? Do you use field mapping?

If you use field mapping your fields should be public (otherwise nobody can access them). If you make them public it works for me. Here's the code as I think it should be:

@Mapper(componentModel = "spring")
public interface MyMapper {

    B mapToB(A a);

    abstract class BaseClass {
        public String updateUser;
    }

    class A extends BaseClass {
        public String updateTime;
    }

    class B extends BaseClass {
        public String updateTime;
    }

}

Result:


@Component
public class MyMapperImpl implements MyMapper {

    @Override
    public B mapToB(A a) {
        if ( a == null ) {
            return null;
        }

        B b = new B();

        b.updateUser = a.updateUser;
        b.updateTime = a.updateTime;

        return b;
    }
}

huangapple
  • 本文由 发表于 2020年8月18日 18:06:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63466378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定