[BAEL-4093] Mapping Collections with MapStruct (#9597)
Co-authored-by: Bogdan Feraru <bogdan.feraru@iquestgroup.com>
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.mapstruct.mappingCollections.dto;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CompanyDTO {
|
||||
|
||||
private List<EmployeeDTO> employees;
|
||||
|
||||
public List<EmployeeDTO> getEmployees() {
|
||||
return employees;
|
||||
}
|
||||
|
||||
public void setEmployees(List<EmployeeDTO> employees) {
|
||||
this.employees = employees;
|
||||
}
|
||||
|
||||
public void addEmployee(EmployeeDTO employeeDTO) {
|
||||
if (employees == null) {
|
||||
employees = new ArrayList<>();
|
||||
}
|
||||
|
||||
employees.add(employeeDTO);
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.mapstruct.mappingCollections.dto;
|
||||
|
||||
public class EmployeeDTO {
|
||||
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.mapstruct.mappingCollections.dto;
|
||||
|
||||
public class EmployeeFullNameDTO {
|
||||
|
||||
private String fullName;
|
||||
|
||||
public String getFullName() {
|
||||
return fullName;
|
||||
}
|
||||
|
||||
public void setFullName(String fullName) {
|
||||
this.fullName = fullName;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.mapstruct.mappingCollections.mapper;
|
||||
|
||||
import com.baeldung.mapstruct.mappingCollections.dto.CompanyDTO;
|
||||
import com.baeldung.mapstruct.mappingCollections.model.Company;
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
@Mapper(uses = EmployeeMapper.class)
|
||||
public interface CompanyMapper {
|
||||
|
||||
CompanyDTO map(Company company);
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.mapstruct.mappingCollections.mapper;
|
||||
|
||||
import com.baeldung.mapstruct.mappingCollections.dto.CompanyDTO;
|
||||
import com.baeldung.mapstruct.mappingCollections.model.Company;
|
||||
import org.mapstruct.CollectionMappingStrategy;
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)
|
||||
public interface CompanyMapperAdderPreferred {
|
||||
|
||||
CompanyDTO map(Company company);
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.mapstruct.mappingCollections.mapper;
|
||||
|
||||
import com.baeldung.mapstruct.mappingCollections.dto.EmployeeFullNameDTO;
|
||||
import com.baeldung.mapstruct.mappingCollections.model.Employee;
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface EmployeeFullNameMapper {
|
||||
|
||||
List<EmployeeFullNameDTO> map(List<Employee> employees);
|
||||
|
||||
default EmployeeFullNameDTO map(Employee employee) {
|
||||
EmployeeFullNameDTO employeeInfoDTO = new EmployeeFullNameDTO();
|
||||
employeeInfoDTO.setFullName(employee.getFirstName() + " " + employee.getLastName());
|
||||
|
||||
return employeeInfoDTO;
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.mapstruct.mappingCollections.mapper;
|
||||
|
||||
import com.baeldung.mapstruct.mappingCollections.dto.EmployeeDTO;
|
||||
import com.baeldung.mapstruct.mappingCollections.model.Employee;
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@Mapper
|
||||
public interface EmployeeMapper {
|
||||
|
||||
List<EmployeeDTO> map(List<Employee> employees);
|
||||
|
||||
Set<EmployeeDTO> map(Set<Employee> employees);
|
||||
|
||||
Map<String, EmployeeDTO> map(Map<String, Employee> idEmployeeMap);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.mapstruct.mappingCollections.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Company {
|
||||
|
||||
private List<Employee> employees;
|
||||
|
||||
public List<Employee> getEmployees() {
|
||||
return employees;
|
||||
}
|
||||
|
||||
public void setEmployees(List<Employee> employees) {
|
||||
this.employees = employees;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.mapstruct.mappingCollections.model;
|
||||
|
||||
public class Employee {
|
||||
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public Employee(String firstName, String lastName) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user