- MapStruct 教程
- MapStruct——主页
- MapStruct——概述
- MapStruct——环境设置
- 映射
- MapStruct——基本映射
- MapStruct——自定义映射
- MapStruct——多重映射
- MapStruct——嵌套 Bean 映射
- MapStruct——直接字段映射
- MapStruct——生成器
- 数据类型转换
- MapStruct——隐式类型转换
- MapStruct——使用 numberFormat
- MapStruct——使用 dateFormat
- MapStruct——使用表达式
- MapStruct——使用常量
- MapStruct——使用 defaultValue
- MapStruct——使用 defaultExpression
- 集合映射
- MapStruct——列表映射
- MapStruct——映射
- 其他
- MapStruct——流映射
- MapStruct——枚举映射
- MapStruct——抛出异常
- MapStruct——自定义映射器
- MapStruct 实用资源
- MapStruct——快速指南
- MapStruct——实用资源
- MapStruct——讨论
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 -
Student.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 -
StudentEntity.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 ...
广告