MapStruct 映射直接字段

MapStruct 可以轻松处理直接字段映射。 例如,将部分作为私有属性的 Student 和将部分作为公共属性的 StudentEntity。 要同时拥有 getter/setter 映射,属性应该是公共的。 在 public final 的情况下,只有 getter 方法将用于映射。

现在创建一个映射器接口。 我们现在将使用 @InheritInverseConfiguration 注解来复制反向配置。

@Mapper
public interface StudentMapper {
   @Mapping(target="className", source="classVal")
   @Mapping(target="subject", source="subject.name")
   Student getModelFromEntity(StudentEntity studentEntity);
    
   @InheritInverseConfiguration
   StudentEntity getEntityFromModel(Student student);
}

示例

打开映射嵌套对象章节中更新的项目映射。

使用以下代码更新 StudentEntity.java

StudentEntity.java

package com.jiyik.entity;

public class StudentEntity {
    private int id;
    private String name;
    private String classVal;
    private SubjectEntity subject;
    public String section;
    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;
    }
    public SubjectEntity getSubject() {
        return subject;
    }
    public void setSubject(SubjectEntity subject) {
        this.subject = subject;
    }
}

使用以下代码更新 Student.java

Student.java

package com.jiyik.model;

public class Student {
    private int id;
    private String name;
    private String className;
    private String subject;
    private String section;
    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;
    }
    public String getSubject() {
        return subject;
    }
    public void setSubject(String subject) {
        this.subject = subject;
    }
    public String getSection() {
        return section;
    }
    public void setSection(String section) {
        this.section = section;
    }
}

使用以下代码更新 StudentMapper.java

StudentMapper.java

package com.jiyik.mapper;

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

@Mapper
public interface StudentMapper {
    @Mapping(target="className", source="classVal")
    @Mapping(target="subject", source="subject.name")
    Student getModelFromEntity(StudentEntity studentEntity);

    @InheritInverseConfiguration
    StudentEntity getEntityFromModel(Student student);
}

使用以下代码更新 StudentMapperTest.java

StudentMapperTest.java

import com.jiyik.entity.StudentEntity;
import com.jiyik.entity.SubjectEntity;
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);
        entity.section = "A";
        SubjectEntity subject = new SubjectEntity();
        subject.setName("Computer");
        entity.setSubject(subject);
        Student model = studentMapper.getModelFromEntity(entity);
        assertEquals(entity.getClassVal(), model.getClassName());
        assertEquals(entity.getName(), model.getName());
        assertEquals(entity.getId(), model.getId());
        assertEquals(entity.getSubject().getName(), model.getSubject());
        assertEquals(entity.section, model.getSection());
    }
    @Test
    public void testModelToEntity() {
        Student model = new Student();
        model.setId(1);
        model.setName("John");
        model.setClassName("X");
        model.setSubject("Science");
        model.setSection("A");
        StudentEntity entity = studentMapper.getEntityFromModel(model);

        assertEquals(entity.getClassVal(), model.getClassName());
        assertEquals(entity.getName(), model.getName());
        assertEquals(entity.getId(), model.getId());
        assertEquals(entity.getSubject().getName(), model.getSubject());
        assertEquals(entity.section, model.getSection());
    }
}

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

$ mvn clean test

执行结果如下

mapstruct 映射直接字段

查看笔记

扫码一下
查看教程更方便