“Kelas Java ModelMapper” Kode Jawaban

Kelas Java ModelMapper

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

Kelas Java ModelMapper

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

Kelas Java ModelMapper

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

Kelas 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

Kelas 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

Kelas 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

Jawaban yang mirip dengan “Kelas Java ModelMapper”

Pertanyaan yang mirip dengan “Kelas Java ModelMapper”

Lebih banyak jawaban terkait untuk “Kelas Java ModelMapper” di Java

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya