- 地图结构教程
- 地图结构 - 主页
- 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 - 使用defaultExpression
使用 Mapstruct,我们可以使用 @Mapping 注释的 defaultExpression 属性在源属性为 null 的情况下使用 defaultExpression 传递计算值。
句法
@Mapping(target = "target-property", source="source-property" defaultExpression = "default-value-method")
这里
default-value-method - 如果源属性为 null,则目标属性将设置为默认值方法的结果。
以下示例演示了相同的内容。
例子
打开项目映射,如在 Eclipse 中使用 defaultValue 映射章节中更新的那样。
使用以下代码更新 CarEntity.java -
CarEntity.java
package com.tutorialspoint.entity; import java.util.GregorianCalendar; public class CarEntity { private int id; private double price; private GregorianCalendar manufacturingDate; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public GregorianCalendar getManufacturingDate() { return manufacturingDate; } public void setManufacturingDate(GregorianCalendar manufacturingDate) { this.manufacturingDate = manufacturingDate; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
使用以下代码更新 Car.java -
汽车.java
package com.tutorialspoint.model; public class Car { private int id; private String price; private String manufacturingDate; private String brand; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getManufacturingDate() { return manufacturingDate; } public void setManufacturingDate(String manufacturingDate) { this.manufacturingDate = manufacturingDate; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
使用以下代码更新 CarMapper.java -
CarMapper.java
package com.tutorialspoint.mapper; import org.mapstruct.Mapper; import org.mapstruct.Mapping; import com.tutorialspoint.entity.CarEntity; import com.tutorialspoint.model.Car; import java.util.UUID; @Mapper( imports = UUID.class ) public interface CarMapper { @Mapping(source = "name", target = "name", defaultExpression = "java(UUID.randomUUID().toString())") @Mapping(target = "brand", constant = "BMW") @Mapping(source = "price", target = "price", numberFormat = "$#.00") @Mapping(source = "manufacturingDate", target = "manufacturingDate", dateFormat = "dd.MM.yyyy") Car getModelFromEntity(CarEntity carEntity); }
使用以下代码更新 CarMapperTest.java -
CarMapperTest.java
package com.tutorialspoint.mapping; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import java.util.GregorianCalendar; import org.junit.jupiter.api.Test; import org.mapstruct.factory.Mappers; import com.tutorialspoint.entity.CarEntity; import com.tutorialspoint.mapper.CarMapper; import com.tutorialspoint.model.Car; public class CarMapperTest { private CarMapper carMapper=Mappers.getMapper(CarMapper.class); @Test public void testEntityToModel() { CarEntity entity = new CarEntity(); entity.setPrice(345000); entity.setId(1); entity.setManufacturingDate(new GregorianCalendar(2015, 3, 5)); Car model = carMapper.getModelFromEntity(entity); assertEquals(model.getPrice(), "$345000.00"); assertEquals(entity.getId(), model.getId()); assertEquals("05.04.2015", model.getManufacturingDate()); assertNotNull(model.getName()); assertEquals("BMW", model.getBrand()); } }
运行以下命令来测试映射。
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.CarMapperTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.035 sec Running com.tutorialspoint.mapping.DeliveryAddressMapperTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec Running com.tutorialspoint.mapping.StudentMapperTest Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec Results : Tests run: 4, Failures: 0, Errors: 0, Skipped: 0 ...