背景介绍
对比几个对象映射框架,MapStruct性能较好,且实现相对容易,网上和官方文档中只支持到jdk1.8,而我们目前的jdk版本为11,网上关于jdk11使用MapStruct的文章很少,有的在自己的项目里也跑不起来,最后在这个例子的指导下实现了 mapstruct-examples
实现方法 Maven配置
<properties>
<org.mapstruct.version>1.4.2.Final</org.mapstruct.version>
<org.projectlombok.version>1.18.22</org.projectlombok.version>
<org.apache.maven.plugins.version>3.8.1</org.apache.maven.plugins.version>
<org.projectlombok.lombok-mapstruct-binding.version>0.2.0</org.projectlombok.lombok-mapstruct-binding.version>
</properties>
<!-- 引入 mapstruct 依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<build>
<plugins>
<!-- 重要的是这个插件配置 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${org.apache.maven.plugins.version}</version>
<configuration>
<!-- We don't need to use the source and target properties because we have just declared maven.compiler.[source|target] property-->
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${org.projectlombok.lombok-mapstruct-binding.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
<showWarnings>true</showWarnings>
<compilerArgs>
<arg>-Amapstruct.suppressGeneratorTimestamp=true</arg>
<arg>-Amapstruct.suppressGeneratorVersionInfoComment=true</arg>
<arg>-Amapstruct.verbose=true</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
简单使用
import org.mapstruct.InheritConfiguration;
import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mappings;
import java.util.Collection;
import java.util.List;
/**
* * @Date: 2022-3-8
* <p></p>
* 基础转换类,提供基本的几个方法,直接继承就可以,如果有需要写Mappings的写在 {@link #to(Object)} 方法上
* 并且接口类上一定要加上 {@link org.mapstruct.Mapper} 注解
* 默认注解,需要单独定义 如 CategoryMapper MAPPER = Mappers.getMapper(CategoryMapper.class); 以此进行实例创建和调用
* 或者如下
*
* @Mapper(componentModel = "spring") 此注解可通过spring进行注入。
*/
public interface BasicObjectMapper<SOURCE, TARGET> {
/**
* 如有需要自定义该注解即可
* 例如:
*
* @Mappings({
* @Mapping(source = "code", target = "categoryCode"),
* @Mapping(source = "name", target = "categoryName")
* })
* <p></p>
* 重写此注解时一定要注意 返回值(TARGET) 和 参数(SOURCE) 的顺序
*/
@Mappings({})
@InheritConfiguration
TARGET to(SOURCE source);
@InheritConfiguration
List<TARGET> to(Collection<SOURCE> source);
@InheritInverseConfiguration
SOURCE from(TARGET source);
@InheritInverseConfiguration
List<SOURCE> from(Collection<TARGET> source);
}
/**
* @Date: 2022-3-8
* @Description: 开发中如需要对象转换操作可直接新建interface并继承BasicObjectMapper<SOURCE,TARGET>,
* 并在新建的接口上加上 @Mapper(componentModel = "spring")
* 字段不一致地方配置mapping:
* @Mappings({
* @Mapping(source = "code", target = "categoryCode"),
* @Mapping(source = "name", target = "categoryName")
* })
* ProductCategory to(CategoryVo source);
* @version: 1.0
*/
//调用商户接口改造,原对象与openfeign对象映射,目前属性都一样
@Mapper(componentModel = "spring")
public interface MerchantMapper extends BasicObjectMapper<MerBusRateCheckForPosReq,Mer0136Param>{
}
网友评论