In this post under MapStruct, I will explain with example how to map attributes from source object to attributes of another existing object.
In all my previous post under MapStruct, when mapping attributes from source object to destination object, the Mapper framework used to create the destination object before mapping.
But there may be scenarios where we need to map the attributes from source object to already existing destination object.
For our example, lets take an example of mapping from “Student” object to “StudentDTO” object.
Below is the class structure of source object
Student
package package6;
public class Student {
private int id;
private String name;
private String className;
//Removed getter, setter, and toString for brevity
}
Below is the class structure for destination object
StudentDTO
package package6;
public class StudentDTO {
private int id;
private String name;
private String className;
//Removed getter, setter, and toString for brevity
}
Now we will come up with Mapper interface as shown below
StudentMapper verison 1
package package6;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
@Mapper
public interface StudentMapper {
void updateStudentDTOFromStudent(Student student, @MappingTarget StudentDTO studentDTO);
}
We can write about interface as shown below
StudentMapper verison 2
package package6;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
@Mapper
public interface StudentMapper {
StudentDTO updateStudentDTOFromStudent(Student student, @MappingTarget StudentDTO studentDTO);
}
In the above code snippet, the following are done.
1) creating an interface
2) annotating the interface with “@Mapper” annotation
3) Adding a method which maps attributes from source object to destination object
4) In previous mapper interfaces we used to returned the destination object and were taking source object as method argument. Here we are taking both source and destination object as method arguments. The return type can be void or destination object itself.
5) We are annotating the destination object with “@MappingTarget” annotation.
This “@MappingTarget” annotation is important here.
This informs the MapStruct framework that the annotated object is already created and it doesn’t has to created again. The MapStruct has to only update it.
Below is the main class that shows how to use the mapper interface.
Main class
1 package package6;
2
3 import org.mapstruct.factory.Mappers;
4
5 public class Example6 {
6 public static void main(String[] args) {
7 Student student = new Student();
8 student.setId(1);
9 student.setName("William Doe");
10 student.setClassName("First");
11
12 StudentDTO studentDTO = new StudentDTO();
13 studentDTO.setId(2);
14 studentDTO.setName("John Doe");
15 studentDTO.setClassName("Second");
16
17 System.out.println("Before updating--> " + studentDTO);
18
19 StudentMapper studentMapper = Mappers.getMapper(StudentMapper.class);
20
21 studentMapper.updateStudentDTOFromStudent(student, studentDTO);
22
23 System.out.println("After updating--> " + studentDTO);
24 }
25 }
In the above code I create an instance of “Student” and “StudentDTO” class and populate it.
At line 19, I get an instance of “StudentMapper” interface by calling “getMapper” method available on “Mappers” interface and sending the “StudentMapper” class object as argument.
At line 21, I call “updateStudentDTOFromStudent” method and pass “Student” and “StudentDTO” instance as arguments to it.
The “StudentDTO” instance is updated with values from “Student” instance.
In this way we can map attributes of source object to already created destination object.
Below is the output
Output
Before updating--> 2:John Doe:Second
After updating--> 1:William Doe:First