Mapstruct 简明教程
MapStruct - Using dateFormat
MapStruct 可以无缝地处理日期到字符串的转换。我们可以在 @Mapping 注解期间将所需格式作为日期格式传递。例如,考虑以下情况,存储在数字中的日期将以特定格式显示。
-
Source - 实体具有日期为 GregorianCalendar(2015, 3, 5)。
-
Target - 将日期显示为 05.04.2015 的模型。
-
dateFormat - 使用格式 dd.MM.yyyy
Example
在 Eclipse 中打开在第 Mapping Using numberFormat 章中更新后的项目映射。
使用以下代码更新 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 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(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 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
Output
命令成功后。验证输出。
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
...