“Klasa Java ModelMapper” Kod odpowiedzi

Klasa Java ModelMapper

<dependency>
  <groupId>org.modelmapper</groupId>
  <artifactId>modelmapper</artifactId>
  <version>2.4.2</version>
</dependency>
Creepy Chicken

Klasa Java ModelMapper

ModelMapper modelMapper = new ModelMapper();
OrderDTO orderDTO = modelMapper.map(order, OrderDTO.class);
Creepy Chicken

Klasa Java ModelMapper

// Assume getters and setters
class OrderDTO {
  String customerFirstName;
  String customerLastName;
  String billingStreet;
  String billingCity;
}
Creepy Chicken

Klasa Java ModelMapper

assertEquals(order.getCustomer().getName().getFirstName(), orderDTO.getCustomerFirstName());
assertEquals(order.getCustomer().getName().getLastName(), orderDTO.getCustomerLastName());
assertEquals(order.getBillingAddress().getStreet(), orderDTO.getBillingStreet());
assertEquals(order.getBillingAddress().getCity(), orderDTO.getBillingCity());
Creepy Chicken

Klasa Java ModelMapper

// Assume getters and setters on each class
class Order {
  Customer customer;
  Address billingAddress;
}

class Customer {
  Name name;
}

class Name {
  String firstName;
  String lastName;
}

class Address {
  String street;
  String city;
}
Creepy Chicken

Klasa Java ModelMapper

modelMapper.typeMap(Order.class, OrderDTO.class).addMappings(mapper -> {
  mapper.map(src -> src.getBillingAddress().getStreet(),
      Destination::setBillingStreet);
  mapper.map(src -> src.getBillingAddress().getCity(),
      Destination::setBillingCity);
});
Creepy Chicken

Odpowiedzi podobne do “Klasa Java ModelMapper”

Pytania podobne do “Klasa Java ModelMapper”

Więcej pokrewnych odpowiedzi na “Klasa Java ModelMapper” w Java

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu