Before starting MapStruct, we need to understand some use cases where MapStruct can be used.
Let's start with entity(source) to DTO(destination/target) mapping which is very common use case in JDBC/JPA/Hibernte projects.
Assume following source(Student) to destination(StudentDto) mapping:
How to map? 🤔
There are multiple ways to map source to destination and the simplest one is to create a method which accept source as request and return destination as response.
public static StudentDTO convertStudentToStudentDTO(Student student) {
if ( student == null ) {
return null;
}
StudentDTO studentDTO = new StudentDTO();
studentDTO.setFirstName(student.getName());
studentDTO.setRollNumber(stdent.getRollNumber());
studentDTO.setEmail(student.getEmail());
return studentDTO;
}
Ok, so what is the big deal here? It's very simple to write such mapping method. Let's suppose there are multiple entitis(source) in your project and below mappings required for each entity:
- Source to Destination mapping (Same as above examble)
- Destination to Source mapping
- List of Source to List of Destination mapping
- List of Destination to List of Source mapping
- Many more methods like update exsing destionation with source
I know still it's not a big deal for many of you to write mapping code for 10-15 classes, but if it is more then this then you have to think for some automatization. 🤔
Another case is when something changed in entity class like field addition/deleteion/data type changed, then you have to change all corresponding methods.
Above are some situations where we need some java bean mapper which can handle things simply, wisely and automatically. To handle these kind of porblem there are multiple Java bean mapper availabe in market and MapStruct is one of them.
MapStruct is a code generator that greatly simplifies the implementation of mappings between Java bean types based on a convention over configuration approach. The generated mapping code uses plain method invocations and thus is fast, type-safe and easy to understand.
This is as per MapStruct officail website.
In simple words MapStruct writes mapping code for you.
This is as per a JAVA DEVELOPER 😝
For example to map Student to StudentDTO, you need to write below code:
@Mapper
public interface StudentMapper {
@Mapping(source = "name", target = "firstName")
StudentDTO convertStudentToStudentDTO(Student student);
}
Code generated by MapStruct:
public class StudentMapperImpl implements StudentMapper {
@Override
public StudentDTO convertStudentToStudentDTO(Student student) {
if ( student == null ) {
return null;
}
StudentDTO studentDTO = new StudentDTO();
studentDTO.setFirstName( student.getName() );
studentDTO.setEmail( student.getEmail() );
studentDTO.setRollNumber( student.getRollNumber() );
return studentDTO;
}
}
Magic 🤓
As discussed above there are many scenarios where we need some mapper to do the mapping task automatically. But the question is why to use MapStruct? So here are some reasons to use MapStruct
- MapStruct generate code at compile time which ensures high performance
- If something is not correctly mapped automatically then developer have option to write custome mapping methods.
- Easy to use
Ther are multiple sites/blogs where you can check comparison results between muliple Java bean mappers.
https://www.baeldung.com/java-performance-mapping-frameworks
https://java.libhunt.com/compare-orika-mapper-vs-mapstruct
I hope now things are clear so let's see how it works >>