MapStruct - 基本映射


使用mapstruct 非常简单。要创建映射器,请在接口上使用org.mapstruct.Msapper注释。

@Mapper
public interface StudentMapper {...}

现在在接口中创建一个转换方法。

@Mapper
public interface StudentMapper {
   Student getModelFromEntity(StudentEntity student);
}

如果源对象属性和目标对象属性具有相同的名称,这些属性将自动映射。如果属性名称不同,请使用 @Mapping 注释,如下所示 -

@Mapper
public interface StudentMapper {
   @Mapping(target="className", source="classVal")
   Student getModelFromEntity(StudentEntity student);
}

这里className是目标对象Student中的属性名称,classVal是源对象StudentEntity中的属性名称。

例子

打开在 Eclipse 中的“环境设置”一章中创建的项目映射。

使用以下代码创建 Student.java -

学生.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;
   }
}

使用以下代码创建 Student.java -

学生实体.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;
   }
}

使用以下代码创建 StudentMapper.java -

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);
}

使用以下代码创建 StudentMapperTest.java -

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;

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());
   }
}

运行以下命令来测试映射。

mvn clean test

输出

一旦命令成功。验证输出。

mvn clean test
[INFO] Scanning for projects...
...
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mapping ---
[INFO] Surefire report directory: \mvn\mapping\target\surefire-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
...