gpt4 book ai didi

java - Orika 映射无法正常工作

转载 作者:行者123 更新时间:2023-12-02 04:29:21 26 4
gpt4 key购买 nike

我已经定义了对象 HomeContentDTO 和 SubscriberUpsertDTO 的映射

public class HomeContentDTO implements Serializable {

private static final long serialVersionUID = 1717544153530572470L;

private String fileName;
private String subscriberName;
private String subscriberEmail;
private String subscriberCompanyName;

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

public String getSubscriberName() {
return subscriberName;
}

public void setSubscriberName(String subscriberName) {
this.subscriberName = subscriberName;
}

public String getSubscriberEmail() {
return subscriberEmail;
}

public void setSubscriberEmail(String subscriberEmail) {
this.subscriberEmail = subscriberEmail;
}

public String getSubscriberCompanyName() {
return subscriberCompanyName;
}

public void setSubscriberCompanyName(String subscriberCompanyName) {
this.subscriberCompanyName = subscriberCompanyName;
}
}


public class SubscriberUpsertDTO implements Serializable{

private static final long serialVersionUID = -6899387128123861532L;
private Integer id;
private String name;
private String email;
private String companyName;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getCompanyName() {
return companyName;
}

public void setCompanyName(String companyName) {
this.companyName = companyName;
}

public boolean isActive() {
return active;
}

public void setActive(boolean active) {
this.active = active;
}
}

以下是两个对象的映射配置

public class HomeMapperRegister {
@MapperRegister
public void register(MapperFactory mapperFactory) {
mapperFactory.classMap(HomeContentDTO.class, SubscriberUpsertDTO.class)
.fieldAToB("subscriberName", "name")
.fieldAToB("subscriberEmail", "email")
.fieldAToB("subscriberCompanyName", "companyName")
.register();
}
}

HomeContentDTO 中的所有映射值不会复制到 SubscriberUpsertDTO。有谁知道是什么原因吗?

SubscriberUpsertDTO subscriberUpsertDTO =     mapperFactory.getMapper().map(homeContentDTO, SubscriberUpsertDTO.class);

最佳答案

class HomeMapperRegister extends ConfigurableMapper {

@Override
public void configure(MapperFactory mapperFactory) {
mapperFactory.classMap(HomeContentDTO.class, SubscriberUpsertDTO.class)
.fieldAToB("subscriberName", "name")
.fieldAToB("subscriberEmail", "email")
.fieldAToB("subscriberCompanyName", "companyName")
.register();
}
}

扩展ConfigurableMapper覆盖configure函数到映射。

关于java - Orika 映射无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31690491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com