- MapStruct 教程
- MapStruct - 首頁
- MapStruct - 概述
- MapStruct - 環境設定
- 對映
- MapStruct - 基本對映
- MapStruct - 自定義對映
- MapStruct - 多重對映
- MapStruct - 對映巢狀 Bean
- MapStruct - 對映直接欄位
- MapStruct - 構建器
- 資料型別轉換
- MapStruct - 隱式型別轉換
- MapStruct - 使用 numberFormat
- MapStruct - 使用 dateFormat
- MapStruct - 使用 expression
- MapStruct - 使用 constant
- MapStruct - 使用 defaultValue
- MapStruct - 使用 defaultExpression
- 對映集合
- MapStruct - 對映列表
- MapStruct - 對映對映
- 其他
- MapStruct - 對映流
- MapStruct - 對映列舉
- MapStruct - 丟擲異常
- MapStruct - 自定義對映器
- MapStruct 有用資源
- MapStruct - 快速指南
- MapStruct - 有用資源
- MapStruct - 討論
MapStruct - 對映直接欄位
MapStruct 可以輕鬆處理直接欄位對映。例如,一個學生有 section 作為私有屬性,一個學生實體有 section 作為公有屬性。要同時進行獲取/設定對映,那麼屬性應當設為公有。如果設為 public final,那麼對映時只有獲取方法可用。
現在建立一個對映器介面。將使用 `@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);
}
示例
在 Eclipse 中開啟在 “對映巢狀物件” 章節中更新的專案對映。
使用以下程式碼更新 `StudentEntity.java` −
`StudentEntity.java`
package com.tutorialspoint.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.tutorialspoint.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.tutorialspoint.mapper;
import org.mapstruct.InheritInverseConfiguration;
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);
@InheritInverseConfiguration
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);
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
輸出
命令成功。驗證輸出。
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 ...
廣告