mapstruct Spring支持的克隆转化
一.版本引用
1.pom.xml文件
<org.mapstruct.version>1.2.0.CR1</org.mapstruct.version> <!--mapStruct依赖 start--> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-jdk8</artifactId> <version>${org.mapstruct.version}</version> </dependency> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${org.mapstruct.version}</version> </dependency> <!--mapStruct依赖 end-->
二.使用
1.pojo对象
package com.sxd.swapping.mybatis.pojo; import lombok.Data; import java.time.LocalDateTime; /** * 数据库表的POJO */ @Data public class DownloadUpload { /** * DB 自增ID */ private Long id; /** * 业务ID */ private Long duId; /** * 业务名称 */ private String duName; /** * 业务状态 枚举值 */ private Integer duStatus; /** * 业务时间 */ private LocalDateTime createTime; }
2.转化对象
package com.sxd.swapping.downloadAndUpload.xlsxDownloadAndUpload.vo; import com.alibaba.excel.annotation.ExcelIgnore; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.write.style.ColumnWidth; import lombok.Data; import java.time.LocalDateTime; @Data @ColumnWidth(25) public class RTReportVO { /** * 业务ID */ @ExcelIgnore private Long duId; /** * 业务ID */ @ExcelProperty(value = "业务ID",index = 0) private String duIdDesc; /** * 业务名称 */ @ExcelProperty(value = "业务名称",index = 1) private String duName; /** * 业务状态 枚举值 */ @ExcelIgnore private Integer duStatus; /** * 业务状态 枚举描述 */ @ExcelProperty(value = "业务状态",index = 2) private String duStatusDesc; /** * 业务时间 DB字段 */ @ExcelIgnore private LocalDateTime createTime; /** * 业务时间 展示字段 */ @ExcelProperty(value = "业务时间",index = 3) private String createTimeDesc; }
3.转化工具定义
package com.sxd.swapping.downloadAndUpload.xlsxDownloadAndUpload.assemble; import com.sxd.swapping.mybatis.pojo.DownloadUpload; import com.sxd.swapping.downloadAndUpload.xlsxDownloadAndUpload.vo.RTReportVO; import org.mapstruct.Mapper; /** * mapstruct * Spring支持的克隆 */ @Mapper(componentModel = "spring") public interface DUDB2VOAssemble { RTReportVO from(DownloadUpload downloadUpload); }
4.使用代码
List<DownloadUpload> dbResult = dudbService.query(duQuery); if (CollectionUtils.isNotEmpty(dbResult)) { List<RTReportVO> reportDTOs = dbResult.stream().map(dudb2VOAssemble::from).collect(Collectors.toList()); }