- 地图结构教程
- 地图结构 - 主页
- 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 - 映射多个对象
我们也可以添加映射多个对象。例如,我们想使用 Student 和 Address 对象获取 DeliveryAddress 对象。
现在创建一个映射器接口,可以将两个对象映射为一个对象。
@Mapper public interface DeliveryAddressMapper { @Mapping(source = "student.name", target = "name") @Mapping(source = "address.houseNo", target = "houseNumber") DeliveryAddress getDeliveryAddress(StudentEntity student, AddressEntity address); }
例子
打开Eclipse 中的自定义映射章节中更新的项目映射。
使用以下代码创建 DeliveryAddress.java -
送货地址.java
package com.tutorialspoint.model; public class DeliveryAddress { private String name; private int houseNumber; private String city; private String state; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getHouseNumber() { return houseNumber; } public void setHouseNumber(int houseNumber) { this.houseNumber = houseNumber; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } }
使用以下代码创建 AddressEntity.java -
地址实体.java
package com.tutorialspoint.entity; public class AddressEntity { private int houseNo; private String city; private String state; public int getHouseNo() { return houseNo; } public void setHouseNo(int houseNo) { this.houseNo = houseNo; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } }
使用以下代码创建 DeliveryAddressMapper.java -
DeliveryAddressMapper.java
package com.tutorialspoint.mapper; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import com.tutorialspoint.entity.AddressEntity; import com.tutorialspoint.entity.StudentEntity; import com.tutorialspoint.model.DeliveryAddress; @Mapper public interface DeliveryAddressMapper { @Mapping(source = "student.name", target = "name") @Mapping(source = "address.houseNo", target = "houseNumber") DeliveryAddress getDeliveryAddress(StudentEntity student, AddressEntity address); }
使用以下代码创建 DeliveryAddressMapperTest.java -
DeliveryAddressMapperTest.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.AddressEntity; import com.tutorialspoint.entity.StudentEntity; import com.tutorialspoint.mapper.DeliveryAddressMapper; import com.tutorialspoint.model.DeliveryAddress; public class DeliveryAddressMapperTest { private DeliveryAddressMapper deliveryAddressMapper = Mappers.getMapper(DeliveryAddressMapper.class); @Test public void testEntityToModel() { StudentEntity student = new StudentEntity(); student.setClassVal("X"); student.setName("John"); student.setId(1); AddressEntity address = new AddressEntity(); address.setCity("Y"); address.setState("Z"); address.setHouseNo(1); DeliveryAddress deliveryAddress = deliveryAddressMapper.getDeliveryAddress(student, address); assertEquals(deliveryAddress.getName(), student.getName()); assertEquals(deliveryAddress.getCity(), address.getCity()); assertEquals(deliveryAddress.getState(), address.getState()); assertEquals(deliveryAddress.getHouseNumber(), address.getHouseNo()); } }
运行以下命令来测试映射。
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.011 sec Running com.tutorialspoint.mapping.StudentMapperTest Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec Results : Tests run: 3, Failures: 0, Errors: 0, Skipped: 0 ...