我正在使用 Selma,我有以下课程:
public class Customer
{
private int id;
private String email;
private String firstName;
private String lastName;
private Date registeredDate;
private List<Address> addresses;
}
我想将其映射到:
public class Customer
{
private String id;
private String email;
private String firstName;
private String lastName;
private Date registeredDate;
private String company;
private Address address1;
private Address address2;
}
有没有办法将 (int)id
转换为 (String)id
并将第一个地址从List
toaddress1
和第二个地址设置为address2
?
我正在考虑使用拦截器,但这样我将不得不手动映射Address
类。有没有办法使用 Selma 自动映射拦截器中的地址类?例如:
public class CustomerCustomMapper
{
public void interceptMyCustomerToCustomer(com.mycode.domain.Customer source, Customer destination) {
if(source.getAddresses() != null && source.getAddresses().size() > 0)
{
com.mycode.domain.Address myAddress1 = source.getAddresses().get(0);
AddressMapper addressMapper = Selma.builder(AddressMapper.class).build();
Address address1 = addressMapper.mapAddress(myAddress1);
destination.setAddress1(address1);
// do the same for address2
}
}
编辑
关于映射地址类,我按照上面显示的方式进行了操作。AddressMapper
我用下面的代码创建了一个:Address toAddress(com.mycode.domain.Address address);
然后我创建了一个 addressMapper 并使用它自动映射地址:
AddressMapper addressMapper = Selma.builder(AddressMapper.class).build();
至于id,目前我必须为我映射的每个类创建一个拦截器(几乎每个类都有一个id(SQL)),并像这样手动设置id:destination.setId(Integer.toString(source.getId()));
实际上这很令人沮丧,但遗憾的是我找不到更好的选择。