名字 |
描述 |
mapstruct |
基于jsr269实现在编译期间生成代码,性能高,精细控制,解耦 |
orika |
能够精细控制,解耦 |
org.springframework.beans.BeanUtils体系 |
简单易用,不能对属性进行定制处理 |
| |
| |
| |
| |
| @Test |
| public void test1() { |
| |
| CarDTO carDTO = buildCarDTO(); |
| |
| 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); |
| } |
| |
| |
| |
| |
| 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") |
| |
| @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); |
| |
| |
| |
| |
| @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); |
| |
| |
| |
| |
| |
| @Mapping(source = "id",target = "driverId") |
| @Mapping(source = "name",target = "fullName") |
| public abstract DriverVO driverDTO2DriverVO(DriverDTO driverDTO); |
| |
| @AfterMapping |
| public void dto2voAfter(CarDTO carDTO,@MappingTarget CarVO carVO) { |
| |
| List<PartDTO> partDTOS = carDTO.getPartDTOS(); |
| boolean hasPart = partDTOS != null && !partDTOS.isEmpty(); |
| carVO.setHasPart(hasPart); |
| } |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下