- MapStruct 教程
- MapStruct - 首頁
- MapStruct - 概述
- MapStruct - 環境設定
- 對映
- MapStruct - 基本對映
- MapStruct - 自定義對映
- MapStruct - 對映多個
- MapStruct - 對映巢狀 Bean
- MapStruct - 對映直接欄位
- MapStruct - 生成器
- 資料型別轉換
- MapStruct - 隱式型別轉換
- MapStruct - 使用 numberFormat
- MapStruct - 透過 dateFormat 來使用
- MapStruct - 使用表示式
- MapStruct - 使用常量
- MapStruct - 使用 defaultValue
- MapStruct - 使用 defaultExpression
- 對映集合
- MapStruct - 對映列表
- MapStruct - 對映對映
- 其他
- MapStruct - 對映流
- MapStruct - 對映列舉
- MapStruct - 丟擲異常
- MapStruct - 自定義對映器
- MapStruct 有用資源
- MapStruct - 快速指南
- MapStruct - 有用資源
- MapStruct - 討論
MapStruct - 透過 dateFormat 來使用
MapStruct 以一種無縫的方式處理將日期轉換為所需格式的字串。我們可以在 @Mapping 註解期間將所需格式作為 dateFormat 傳遞。例如,考慮將儲存為數字的日期顯示為特定格式的情況。
源 - 實體的日期是 GregorianCalendar(2015, 3, 5)。
目標 - 模型以 05.04.2015 格式顯示日期。
dateFormat - 使用格式 dd.MM.yyyy
示例
在 Eclipse 中開啟對映專案,如 使用 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
輸出
命令成功後,驗證輸出。
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 ...
廣告