- MapStruct 教程
- MapStruct - 首页
- MapStruct - 概述
- MapStruct - 环境设置
- 映射
- MapStruct - 基本映射
- MapStruct - 自定义映射
- MapStruct - 多个映射
- MapStruct - 嵌套 Bean 映射
- MapStruct - 直接字段映射
- MapStruct - 构建器
- 数据类型转换
- MapStruct - 隐式类型转换
- MapStruct - 使用 numberFormat
- MapStruct - 使用 dateFormat
- MapStruct - 使用表达式
- MapStruct - 使用常量
- MapStruct - 使用 defaultValue
- MapStruct - 使用 defaultExpression
- 集合映射
- MapStruct - 列表映射
- MapStruct - Map 映射
- 其他
- MapStruct - 流映射
- MapStruct - 枚举映射
- MapStruct - 抛出异常
- MapStruct - 自定义映射器
- MapStruct 有用资源
- MapStruct - 快速指南
- MapStruct - 有用资源
- MapStruct - 讨论
MapStruct - 使用表达式
MapStruct 允许为自定义逻辑调用转换方法。我们可以使用表达式来实现相同的功能,其中我们可以传递任何 Java 对象并调用其方法来进行转换。
语法
@Mapping(target = "target-property", expression = "java(target-method())")
这里
目标属性 - 我们正在进行映射的属性。
表达式 - 映射器将调用表达式中编写的 Java 方法。
目标方法 - 目标方法是要调用的方法。如果方法存在于不同的类中,则使用 new 类名.目标方法()
示例
在 Eclipse 中打开在 使用 dateFormat 映射 章节中更新的项目映射。
使用以下代码更新 CarEntity.java -
CarEntity.java
package com.tutorialspoint.entity;
import java.util.GregorianCalendar;
public class CarEntity {
private int id;
private double price;
private GregorianCalendar manufacturingDate;
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;
}
}
使用以下代码更新 Car.java -
Car.java
package com.tutorialspoint.model;
public class Car {
private int id;
private String price;
private String manufacturingDate;
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;
}
}
使用以下代码更新 CarMapper.java -
CarMapper.java
package com.tutorialspoint.mapper;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import com.tutorialspoint.entity.CarEntity;
import com.tutorialspoint.model.Car;
@Mapper
public interface CarMapper {
@Mapping(source = "price", target = "price", numberFormat = "$#.00")
@Mapping(target = "manufacturingDate",
expression = "java(getManufacturingDate(carEntity.getManufacturingDate()))")
Car getModelFromEntity(CarEntity carEntity);
default String getManufacturingDate(GregorianCalendar manufacturingDate) {
Date d = manufacturingDate.getTime();
SimpleDateFormat sdf = new SimpleDateFormat( "dd.MM.yyyy" );
return sdf.format( d );
}
}
使用以下代码更新 CarMapperTest.java -
CarMapperTest.java
package com.tutorialspoint.mapping;
import static org.junit.jupiter.api.Assertions.assertEquals;
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());
}
}
运行以下命令来测试映射。
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 ...
广告