1

私はセルマを使用しており、次のクラスがあります。

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;
}

id(int)を (String)にキャストしid、最初のアドレスをListtoaddress1に、2 番目のアドレスをに設定する方法はあり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
        }
    }

編集

Address クラスのマッピングに関しては、上で示した方法で行いました。AddressMapper内部に次のコードを含む を作成しました。Address toAddress(com.mycode.domain.Address address);

次に、addressMapper を作成し、それを使用して Address を自動的にマップします。

AddressMapper addressMapper = Selma.builder(AddressMapper.class).build();

IDに関しては、現在、マップするすべてのクラスのインターセプターを作成する必要があり(ほとんどすべてのクラスにIDが含まれています(SQL))、次のように手動でIDを設定する必要があります。destination.setId(Integer.toString(source.getId()));

実際にはかなりイライラしますが、悲しいことに、より良いオプションが見つかりません。

4

1 に答える 1

1

for the list to Address the interceptor is the good answer.

For the int to String you can specify a custom mapper mapping from int to String (see http://www.selma-java.org/#custom-mapper).

But I would recommend using an abstract mapper instead of building a new mapper to map the Address. This way you'll be able to integrate your specific code inside the mapper and call the address mapper method directly (see http://www.selma-java.org/#abstract-mapper).

For the toString thing, you can also add a feature request to Github. I'll be glad to add this feature.

于 2016-02-18T11:07:54.760 に答える