引入mapstruct依赖
<dependency><groupId>org.mapstruct</groupId><artifactId>mapstruct</artifactId><version>${org.mapstruct.version}</version> </dependency>
配置maven-compiler-plugin
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.5</version><configuration><source>${java.version}</source><target>${java.version}</target><encoding>UTF-8</encoding><annotationProcessorPaths><path><groupId>org.mapstruct</groupId><artifactId>mapstruct-processor</artifactId><version>${org.mapstruct.version}</version></path><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version></path><path><groupId>org.projectlombok</groupId><artifactId>lombok-mapstruct-binding</artifactId><version>${mapstruct-binding.version}</version></path></annotationProcessorPaths></configuration></plugin></plugins> </build>
附:版本
<org.mapstruct.version>1.4.2.Final</org.mapstruct.version>
<lombok.version>1.18.22</lombok.version>
<mapstruct-binding.version>0.2.0</mapstruct-binding.version>
代码示例
@Mapper
public interface NodeMessageConverter {/*** 转换实例*/NodeMessageConverter INSTANCE = Mappers.getMapper(NodeMessageConverter.class);/*** 审批消息转换*/ApprovalMessageEntity convertApprovalMessage(NodeMessageParam messageParam);/*** 采购消息转换*/PurchaseMessageEntity convertPurchaseMessage(NodeMessageParam messageParam);
}// 应用
PurchaseMessageEntity purchaseMessageEntity = NodeMessageConverter.INSTANCE.convertPurchaseMessage(messageParam);