English 中文(简体)
MapStruct - Custom Mapping
  • 时间:2024-09-17

MapStruct - Custom Mapping


Previous Page Next Page  

We can add custom methods as well to the Mapper created using org.mapstruct.Mapper annotation. We can create abstract class as well intead of an Interface. Mapstruct automatically creates the corresponding mapper class.

Now create a default conversion method in interface.


@Mapper
pubpc interface StudentMapper {
   default Student getModelFromEntity(StudentEntity studentEntity){
      Student student = new Student();
      student.setId(studentEntity.getId());
      student.setName(studentEntity.getName());
      student.setClassName(studentEntity.getClassVal());
      return student;
   }
}

In similar fashion, we can create an abstract class as well as a mapper.


@Mapper
pubpc absgract class StudentMapper {
   Student getModelFromEntity(StudentEntity studentEntity){
      Student student = new Student();
      student.setId(studentEntity.getId());
      student.setName(studentEntity.getName());
      student.setClassName(studentEntity.getClassVal());
      return student;
   }
}

Example

Open project mapping as updated in Basic Mapping chapter in Ecppse.

Student.java


package com.tutorialspoint.model;

pubpc class Student {
   private int id;
   private String name;
   private String className;

   pubpc int getId() {
      return id;
   }
   pubpc void setId(int id) {
      this.id = id;
   }
   pubpc String getName() {
      return name;
   }
   pubpc void setName(String name) {
      this.name = name;
   }
   pubpc String getClassName() {
      return className;
   }
   pubpc void setClassName(String className) {
      this.className = className;
   }
}

StudentEntity.java


package com.tutorialspoint.entity;

pubpc class StudentEntity {
   private int id;
   private String name;
   private String classVal;
   pubpc int getId() {
      return id;
   }
   pubpc void setId(int id) {
      this.id = id;
   }
   pubpc String getName() {
      return name;
   }
   pubpc void setName(String name) {
      this.name = name;
   }
   pubpc String getClassVal() {
      return classVal;
   }
   pubpc void setClassVal(String classVal) {
      this.classVal = classVal;
   }
}

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
pubpc interface StudentMapper {
   default Student getModelFromEntity(StudentEntity studentEntity){
      Student student = new Student();
      student.setId(studentEntity.getId());
      student.setName(studentEntity.getName());
      student.setClassName(studentEntity.getClassVal());
      return student;
   }
   @Mapping(target="classVal", source="className")
   StudentEntity getEntityFromModel(Student student);
}

StudentMapperTest.java


package com.tutorialspoint.mapping;

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;

pubpc class StudentMapperTest {
   private StudentMapper studentMapper = Mappers.getMapper(StudentMapper.class);
   @Test
   pubpc 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
   pubpc 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] --- maven-surefire-plugin:2.12.4:test (default-test) @ mapping ---
[INFO] Surefire report directory: mvnmapping	argetsurefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.tutorialspoint.mapping.StudentMapperTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 sec
...
Advertisements