
- MapStruct 教程
- MapStruct——主頁
- MapStruct——概述
- MapStruct——環境設定
- 對映
- MapStruct——基本對映
- MapStruct——自定義對映
- MapStruct——多重對映
- MapStruct——巢狀 Bean 的對映
- MapStruct——直接欄位的對映
- MapStruct——Builder
- 資料型別轉換
- MapStruct——隱式型別轉換
- MapStruct——使用 numberFormat
- MapStruct——使用 dateFormat
- MapStruct——使用表示式
- MapStruct——使用常量
- MapStruct——使用 defaultValue
- MapStruct——使用 defaultExpression
- 對映集合
- MapStruct——對映 List
- MapStruct——對映 Map
- 其他
- MapStruct——對映 Streams
- MapStruct——對映 Enum
- MapStruct——丟擲異常
- MapStruct——自定義對映器
- MapStruct 實用的資源
- MapStruct——快速指南
- MapStruct——有用的資源
- MapStruct——討論
MapStruct——利用 Builder
MapStruct 允許使用 Builder。我們可以使用 Builder 框架或自定義 Builder。在下面的示例中,我們使用自定義 Builder。
示例
在 Eclipse 中以 “對映直接欄位”章節更新的形式開啟專案對映。
使用以下程式碼更新 Student.java −
Student.java
package com.tutorialspoint.model; public class Student { private final String name; private final int id; protected Student(Student.Builder builder) { this.name = builder.name; this.id = builder.id; } public static Student.Builder builder() { return new Student.Builder(); } public static class Builder { private String name; private int id; public Builder name(String name) { this.name = name; return this; } public Builder id(int id) { this.id = id; return this; } public Student create() { return new Student( this ); } } public String getName() { return name; } public int getId() { return id; } }
使用以下程式碼更新 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 { Student getModelFromEntity(StudentEntity studentEntity); @Mapping(target="id", source="id") @Mapping(target="name", source="name") 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.setName("John"); entity.setId(1); Student model = studentMapper.getModelFromEntity(entity); assertEquals(entity.getName(), model.getName()); assertEquals(entity.getId(), model.getId()); } @Test public void testModelToEntity() { Student.Builder builder = Student.builder().id(1).name("John"); Student model = builder.create(); StudentEntity entity = studentMapper.getEntityFromModel(model); 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.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 ...
廣告