MapStruct - Builder



MapStruct allows to use Builders. We can use Builder frameworks or can use our custom builder. In below example, we are using a custom builder.

Example

Open project mapping as updated in Mapping Direct Fields chapter in Eclipse.

Update Student.java with following code −

Student.java

package com.tutorialspoint.model; public class Student { private final String name; private final int id; protected Student(Student.Builder builder) { this.name = builder.name; this.id = builder.id; } public static Student.Builder builder() { return new Student.Builder(); } public static class Builder { private String name; private int id; public Builder name(String name) { this.name = name; return this; } public Builder id(int id) { this.id = id; return this; } public Student create() { return new Student( this ); } } public String getName() { return name; } public int getId() { return id; } }

Update StudentMapper.java with following code −

StudentMapper.java

package com.tutorialspoint.mapper; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import com.tutorialspoint.entity.StudentEntity; import com.tutorialspoint.model.Student; @Mapper public interface StudentMapper { Student getModelFromEntity(StudentEntity studentEntity); @Mapping(target="id", source="id") @Mapping(target="name", source="name") StudentEntity getEntityFromModel(Student student); }

Update StudentMapperTest.java with following code −

StudentMapperTest.java

package com.tutorialspoint.test; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; import org.mapstruct.factory.Mappers; import com.tutorialspoint.entity.StudentEntity; import com.tutorialspoint.mapper.StudentMapper; import com.tutorialspoint.model.Student; public class StudentMapperTest { private StudentMapper studentMapper = Mappers.getMapper(StudentMapper.class); @Test public void testEntityToModel() { StudentEntity entity = new StudentEntity(); entity.setName("John"); entity.setId(1); Student model = studentMapper.getModelFromEntity(entity); assertEquals(entity.getName(), model.getName()); assertEquals(entity.getId(), model.getId()); } @Test public void testModelToEntity() { Student.Builder builder = Student.builder().id(1).name("John"); Student model = builder.create(); StudentEntity entity = studentMapper.getEntityFromModel(model); assertEquals(entity.getName(), model.getName()); assertEquals(entity.getId(), model.getId()); } }

Run the following command to test the mappings.

mvn clean test

Output

Once command is successful. Verify the output.

mvn clean test
[INFO] Scanning for projects...
...
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.tutorialspoint.test.[1mDeliveryAddressMapperTest[m
[INFO] [1;32mTests run: [0;1;32m1[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.035 s -- in com.tutorialspoint.test.[1mDeliveryAddressMapperTest[m
[INFO] Running com.tutorialspoint.test.[1mStudentMapperTest[m
[INFO] [1;32mTests run: [0;1;32m2[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.009 s -- in com.tutorialspoint.test.[1mStudentMapperTest[m
[INFO] 
[INFO] Results:
[INFO] 
[INFO] [1;32mTests run: 3, Failures: 0, Errors: 0, Skipped: 0[m
[INFO] 
[INFO] [1m------------------------------------------------------------------------[m
[INFO] [1;32mBUILD SUCCESS[m
[INFO] [1m------------------------------------------------------------------------[m
[INFO] Total time:  3.665 s
[INFO] Finished at: 2025-09-13T17:07:08+05:30
[INFO] [1m------------------------------------------------------------------------[m
Advertisements