- 地图结构教程
- 地图结构 - 主页
- MapStruct - 概述
- MapStruct - 环境设置
- 测绘
- MapStruct - 基本映射
- MapStruct - 自定义映射
- MapStruct - 映射多个
- MapStruct - 映射嵌套 Bean
- MapStruct - 映射直接字段
- MapStruct - 生成器
- 数据类型转换
- MapStruct - 隐式类型转换
- MapStruct - 使用 numberFormat
- MapStruct - 使用日期格式
- MapStruct - 使用表达式
- MapStruct - 使用常量
- MapStruct - 使用defaultValue
- MapStruct - 使用defaultExpression
- 映射集合
- MapStruct - 映射列表
- MapStruct - 映射地图
- 各种各样的
- MapStruct - 映射流
- MapStruct - 映射枚举
- MapStruct - 抛出异常
- MapStruct - 自定义映射器
- MapStruct 有用资源
- MapStruct - 快速指南
- MapStruct - 有用的资源
- MapStruct - 讨论
MapStruct - 映射嵌套 Bean
MapStruct 无缝处理嵌套映射。例如,一个主题为嵌套 bean 的学生。
现在创建一个可以映射嵌套对象的映射器接口。
@Mapper public interface StudentMapper { @Mapping(target="className", source="classVal") @Mapping(target="subject", source="subject.name") Student getModelFromEntity(StudentEntity studentEntity); @Mapping(target="classVal", source="className") @Mapping(target="subject.name", source="subject") StudentEntity getEntityFromModel(Student student); }
例子
打开项目映射,如Eclipse 中的“映射多个对象”一章中更新的那样。
使用以下代码创建SubjectEntity.java -
主题实体.java
package com.tutorialspoint.entity; public class SubjectEntity { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
使用以下代码更新 StudentEntity.java -
学生实体.java
package com.tutorialspoint.entity; public class StudentEntity { private int id; private String name; private String classVal; private SubjectEntity subject; 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 -
学生.java
package com.tutorialspoint.model; public class Student { private int id; private String name; private String className; private String subject; 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; } }
使用以下代码更新 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") @Mapping(target="subject", source="subject.name") Student getModelFromEntity(StudentEntity studentEntity); @Mapping(target="classVal", source="className") @Mapping(target="subject.name", source="subject") 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.entity.SubjectEntity; 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); 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()); } @Test public void testModelToEntity() { Student model = new Student(); model.setId(1); model.setName("John"); model.setClassName("X"); model.setSubject("Science"); 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()); } }
运行以下命令来测试映射。
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.DeliveryAddressMapperTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec Running com.tutorialspoint.mapping.StudentMapperTest Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec Results : Tests run: 3, Failures: 0, Errors: 0, Skipped: 0 ...