英文:
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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论