- MapStruct - Home
- MapStruct - Overview
- MapStruct - Environment Setup
- MapStruct - Mapping
- MapStruct - Basic Mapping
- MapStruct - Custom Mapping
- MapStruct - Mapping Multiple
- MapStruct - Mapping Nested Bean
- MapStruct - Mapping Direct Field
- MapStruct - Builder
- MapStruct - Data Type Conversions
- MapStruct - Implicit Type Conversion
- MapStruct - Using numberFormat
- MapStruct - Using dateFormat
- MapStruct - Using expression
- MapStruct - Using constant
- MapStruct - Using defaultValue
- MapStruct - Using defaultExpression
- MapStruct - Mapping Collections
- MapStruct - Mapping List
- MapStruct - Mapping Map
- Mapstruct - Miscellaneous
- MapStruct - Mapping Streams
- MapStruct - Mapping Enum
- MapStruct - Throwing Exceptions
- MapStruct - Customizing Mapper
- MapStruct - Useful Resources
- MapStruct - Quick Guide
- MapStruct - Useful Resources
- MapStruct - Discussion
MapStruct - Basic Mapping
Using mapstruct is very easy. To create a mapper use org.mapstruct.Mapper annotation on an interface.
Syntax
@Mapper
public interface StudentMapper {...}
Now create a conversion method in interface.
@Mapper
public interface StudentMapper {
Student getModelFromEntity(StudentEntity student);
}
In case both source and target object properties have same name, those properties will be mapped automatically. In case property name is different, use the @Mapping annotation as following −
@Mapper
public interface StudentMapper {
@Mapping(target="className", source="classVal")
Student getModelFromEntity(StudentEntity student);
}
Here className is the property name in Student, a target object and classVal is the property name in StudentEntity, a source object.
Example - Usage of Mapper to map entities
Open project mapping as created in Environment Setup chapter in Eclipse.
Create Student.java with following code −
Student.java
package com.tutorialspoint.model;
public class Student {
private int id;
private String name;
private String className;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
Create Student.java with following code −
StudentEntity.java
package com.tutorialspoint.entity;
public class StudentEntity {
private int id;
private String name;
private String classVal;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassVal() {
return classVal;
}
public void setClassVal(String classVal) {
this.classVal = classVal;
}
}
Create 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 {
@Mapping(target="className", source="classVal")
Student getModelFromEntity(StudentEntity student);
@Mapping(target="classVal", source="className")
StudentEntity getEntityFromModel(Student student);
}
Create 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.setClassVal("X");
entity.setName("John");
entity.setId(1);
Student model = studentMapper.getModelFromEntity(entity);
assertEquals(entity.getClassVal(), model.getClassName());
assertEquals(entity.getName(), model.getName());
assertEquals(entity.getId(), model.getId());
}
@Test
public void testModelToEntity() {
Student model = new Student();
model.setId(1);
model.setName("John");
model.setClassName("X");
StudentEntity entity = studentMapper.getEntityFromModel(model);
assertEquals(entity.getClassVal(), model.getClassName());
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.[1mStudentMapperTest[m [INFO] [1;32mTests run: [0;1;32m2[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.029 s -- in com.tutorialspoint.test.[1mStudentMapperTest[m [INFO] [INFO] Results: [INFO] [INFO] [1;32mTests run: 2, Failures: 0, Errors: 0, Skipped: 0[m [INFO] [INFO] [1m------------------------------------------------------------------------[m [INFO] [1;32mBUILD SUCCESS[m [INFO] [1m------------------------------------------------------------------------[m [INFO] Total time: 5.685 s [INFO] Finished at: 2025-09-12T16:58:15+05:30 [INFO] [1m------------------------------------------------------------------------[m