名字 | 描述 |
mapstruct | 基于jsr269實作在編譯期間生成代碼,性能高,精細控制,解耦 |
orika | 能夠精細控制,解耦 |
org.springframework.beans.BeanUtils體系 |
/**
* 測試傳統的通過getter和setter指派完成pojo之間轉換
* CarDto-->CarVo
*/
@Test
public void test1() {
// 模拟業務構造出的CarDTO對象
CarDTO carDTO = buildCarDTO();
// 轉化dto-vo
CarVO carVO = new CarVO();
carVO.setId(carDTO.getId());
carVO.setVin(carDTO.getVin());
carVO.setPrice(carDTO.getPrice()); // 裝箱拆箱機制,不需要我們自己轉化
double totalPrice = carDTO.getTotalPrice();
DecimalFormat df = new DecimalFormat("#.00");
String totalPriceStr = df.format(totalPrice);
carVO.setTotalPrice(totalPriceStr);
Date publishDate = carDTO.getPublishDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String publishDateFormat = sdf.format(publishDate);
carVO.setPublishDate(publishDateFormat);
carVO.setBrandName(carDTO.getBrand());
List<PartDTO> partDTOS = carDTO.getPartDTOS();
boolean hasPart = partDTOS != null && !partDTOS.isEmpty();
carVO.setHasPart(hasPart);
DriverVO driverVO = new DriverVO();
DriverDTO driverDTO = carDTO.getDriverDTO();
driverVO.setDriverId(driverDTO.getId());
driverVO.setFullName(driverDTO.getName());
carVO.setDriverVO(driverVO);
System.out.println(carVO);
}
/**
* 模拟業務構造出的CarDTO對象
*/
private CarDTO buildCarDTO() {
CarDTO carDTO = new CarDTO();
carDTO.setId(330L);
carDTO.setVin("vin123456789");
carDTO.setPrice(123789.126d);
carDTO.setTotalPrice(143789.126d);
carDTO.setPublishDate(new Date());
carDTO.setColor("白色");
carDTO.setBrand("大衆");
// 零件
PartDTO partDTO1 = new PartDTO();
partDTO1.setPartId(1L);
partDTO1.setPartName("多功能方向盤");
PartDTO partDTO2 = new PartDTO();
partDTO2.setPartId(2L);
partDTO2.setPartName("智能車門");
List<PartDTO> partDTOList = new ArrayList<>();
partDTOList.add(partDTO1);
partDTOList.add(partDTO2);
carDTO.setPartDTOS(partDTOList);
// 設定駕駛員
DriverDTO driverDTO = new DriverDTO();
driverDTO.setId(88L);
driverDTO.setName("小明");
carDTO.setDriverDTO(driverDTO);
return carDTO;
}
# 引入依賴
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</dependency>
# 編寫convert
import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper
public abstract class CarConvert {
public static CarConvert INSTANCE = Mappers.getMapper(CarConvert.class);
public abstract CarVO dto2vo(CarDTO carDTO);
}
# 測試
@Test
public void test2() {
CarDTO carDTO = buildCarDTO();
CarVO carVO = CarConvert.INSTANCE.dto2vo(carDTO);
System.out.println(carVO);
}
同類型且同名的屬性,會自動映射
mapstruct會自動進行類型轉換
8種基本類型和他們對應的包裝類
8種基本類型(含包裝類)和string之間
日期類型和string
指定屬性之間的映射關系
數字格式化:numberFormat = "#.00"
日期格式化:dateFormat = "yyyy-MM-dd HH:mm:ss"
source或target多餘的屬性對方沒有,不會報錯的
ignore
源屬性不想映射到目标屬性,使用該配置,目标屬性顯示null
屬性是引用對象的映射處理
@Mapping(source = "driverDTO",target = "driverVO") // 并寫上對應的abstract方法
@Mapping(source = "id",target = "driverId")
@Mapping(source = "name",target = "fullName")
public abstract DriverVO driverDTO2DriverVO(DriverDTO driverDTO);
@AfterMapping和@MappingTarget
在映射最後一步對屬性的自定義映射處理
@Mapper
public abstract class CarConvert {
public static CarConvert INSTANCE = Mappers.getMapper(CarConvert.class);
/**
* CarDto-->CarVo
*/
@Mappings(
value = {
@Mapping(source = "totalPrice",target = "totalPrice",numberFormat = "#.00"), // 數字格式化
@Mapping(source = "publishDate",target = "publishDate",dateFormat = "yyyy-MM-dd HH:mm:ss"), // 日期格式化
@Mapping(target = "color",ignore = true), // 源屬性不想映射到目标屬性
@Mapping(source = "brand",target = "brandName"), // 源屬性與目标屬性名稱不一緻
@Mapping(source = "driverDTO",target = "driverVO") // 源屬性與目标屬性都是應用類型
}
)
public abstract CarVO dto2vo(CarDTO carDTO);
/**
* driverDTO-->driverVO
* @param driverDTO
*/
@Mapping(source = "id",target = "driverId")
@Mapping(source = "name",target = "fullName")
public abstract DriverVO driverDTO2DriverVO(DriverDTO driverDTO);
@AfterMapping // 表示讓mapstruct在調用完自動轉換的方法之後,會來自動調用本方法
public void dto2voAfter(CarDTO carDTO,@MappingTarget CarVO carVO) {
// @MappingTarget : 表示傳來的carVO對象是已經指派過的
List<PartDTO> partDTOS = carDTO.getPartDTOS();
boolean hasPart = partDTOS != null && !partDTOS.isEmpty();
carVO.setHasPart(hasPart);
}
}