Assume I have an Employee POJO:
package com.example.demo.vo; import com.example.demo.enumeration.Gender; import com.example.demo.enumeration.YesOrNoType; import lombok.Data; import lombok.ToString; @Data @ToString public class Employee { /** * 員工編號 */ private Integer id; /** * 員工姓名 */ private String name; /** * 員工性別 */ private Gender gender; /** * 是否離職 */ private YesOrNoType quit; }
and an EmployeeDto POJO:
package com.example.demo.vo; import lombok.Data; @Data public class EmployeeDto { /** * 員工編號 */ private String id; /** * 員工姓名 */ private String name; /** * 員工性別 */ private String gender; /** * 是否離職 */ private String quit; }
Gender Enumeration:
package com.example.demo.enumeration; public enum Gender { MALE("M", "男"), FEMALE("F", "女"); private final String value; private final String description; Gender(String value, String description) { this.value = value; this.description = description; } public String getValue() { return value; } public String getDescription() { return description; } }
YesOrNoType Enumeration:
package com.example.demo.enumeration; public enum YesOrNoType { YES("Y", "是"), NO("N", "否"); private final String value; private final String description; YesOrNoType(String value, String description) { this.value = value; this.description = description; } public String getValue() { return value; } public String getDescription() { return description; } }
If I would like to convert Employee to EmployeeDTO, and convert gender and quit attributes to description, how to do it?
How-To
Add the following configurations in your pom.xml
<properties> <org.mapstruct.version>1.2.0.Final</org.mapstruct.version> </properties> <dependencies> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> <!-- use mapstruct-jdk8 for Java 8 or higher --> <version>${org.mapstruct.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <!-- or higher, depending on your project --> <target>1.8</target> <!-- or higher, depending on your project --> <annotationProcessorPaths> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${org.mapstruct.version}</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build>
Create an EmployeeMapper:
package com.example.demo.mapStruct; import org.mapstruct.DecoratedWith; import org.mapstruct.Mapper; import com.example.demo.vo.Employee; import com.example.demo.vo.EmployeeDto; @Mapper @DecoratedWith(EmployeeDecorator.class) public interface EmployeeMapper { EmployeeDto toDto(Employee employee); }
Create an EmployeeDecorator:
package com.example.demo.mapStruct; import com.example.demo.vo.Employee; import com.example.demo.vo.EmployeeDto; public class EmployeeDecorator implements EmployeeMapper { private EmployeeMapper delegate; public EmployeeDecorator(EmployeeMapper delegate) { super(); this.delegate = delegate; } @Override public EmployeeDto toDto(Employee employee) { EmployeeDto dto = delegate.toDto(employee); dto.setId(employee.getId().toString()); dto.setGender(employee.getGender().getDescription()); dto.setQuit(employee.getQuit().getDescription()); return dto; } }
Create a test class:
package com.example.demo; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.mapstruct.factory.Mappers; import com.example.demo.enumeration.Gender; import com.example.demo.enumeration.YesOrNoType; import com.example.demo.mapStruct.EmployeeMapper; import com.example.demo.vo.Employee; import com.example.demo.vo.EmployeeDto; import lombok.extern.slf4j.Slf4j; @Slf4j public class MapStructTest { private static List<Employee> employees = new ArrayList<>(); static { Employee albert = new Employee(); albert.setId(1); albert.setName("Albert"); albert.setGender(Gender.MALE); albert.setQuit(YesOrNoType.NO); Employee mandy = new Employee(); mandy.setId(2); mandy.setName("Mandy"); mandy.setGender(Gender.FEMALE); mandy.setQuit(YesOrNoType.NO); Employee peter = new Employee(); peter.setId(3); peter.setName("Peter"); peter.setGender(Gender.MALE); peter.setQuit(YesOrNoType.YES); employees = Arrays.asList(albert, mandy, peter); } public static void main(String[] args) { EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class); for (Employee employee : employees) { EmployeeDto dto = mapper.toDto(employee); log.debug("employee: " + employee.toString() + ", dto : " + dto.toString()); } } }
10:51:10.018 [main] DEBUG com.example.demo.MapStructTest - employee: Employee(id=1, name=Albert, gender=MALE, quit=NO), dto : EmployeeDto(id=1, name=Albert, gender=男, quit=否) 10:51:10.020 [main] DEBUG com.example.demo.MapStructTest - employee: Employee(id=1, name=Mandy, gender=FEMALE, quit=NO), dto : EmployeeDto(id=2, name=Mandy, gender=女, quit=否) 10:51:10.020 [main] DEBUG com.example.demo.MapStructTest - employee: Employee(id=3, name=Peter, gender=MALE, quit=YES), dto : EmployeeDto(id=3, name=Peter, gender=男, quit=是)
No comments:
Post a Comment