MapStruct 自定义映射

我们也可以向使用 org.mapstruct.Mapper 注解创建的 Mapper 添加自定义方法。 我们也可以创建抽象类而不是接口。 Mapstruct 会自动创建相应的映射器类。

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

@Mapper
public 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;
   }
}

以类似的方式,我们可以创建一个抽象类以及一个映射器。

@Mapper
public 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;
   }
}

示例

打开 MapStruct 基本映射 章节中更新的项目映射。

Student.java

package com.jiyik.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;
    }
}

StudentEntity.java

package com.jiyik.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

package com.jiyik.mapper;

import com.jiyik.entity.StudentEntity;
import com.jiyik.model.Student;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper
public 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

import com.jiyik.entity.StudentEntity;
import com.jiyik.mapper.StudentMapper;
import com.jiyik.model.Student;
import org.junit.Test;
import org.mapstruct.factory.Mappers;

import static org.junit.Assert.assertEquals;

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

执行结果如下

MapStruct 基础映射

查看笔记

扫码一下
查看教程更方便