背景
Mapstruct是一种实体类映射框架,能够通过Java注解将一个实体类的属性安全地赋值给另一个实体类。有了mapstruct,只需要定义一个映射器接口,声明需要映射的方法,在编译过程中,mapstruct会自动生成该接口的实现类,实现将源对象映射到目标对象的效果。
MapStruct性能较好,且实现相对容易,网上和官方文档中只支持到jdk1.8,以下操作为jdk11使用MapStruct的流程。
1、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>
<dependencies>
<!-- 引入 lombok 依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</dependency>
<!-- 引入 mapstruct 依赖 -->
<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>
</dependencies>
<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>
2、代码举例
import com.shanjupay.merchant.api.dto.MerchantDTO;
import com.shanjupay.merchant.entity.Merchant;
import org.mapstruct.Mapper; //mapper注解,能帮助主动创建接口实现类
import org.mapstruct.factory.Mappers;//可以通过class文件获取到对象
import java.util.ArrayList;
import java.util.List;
/**
* 定义dto和entity之间的转换规则
*/
@org.mapstruct.Mapper //对象属性的映射
public interface MerchantConvert {
//接口成员变量只能是常量
//转换类实例
//Mappers.getMapper(接口class文件),可以获取到接口的实现类
MerchantConvert INSTANCE = Mappers.getMapper(MerchantConvert.class);
//把dto转换成entity
Merchant dto2entity(MerchantDTO merchantDTO);
//把entity转换成dto
MerchantDTO entity2dto(Merchant merchant);
//list之间也可以转换,将entity的list转换成MerchantDTO list
List<MerchantDTO> entityList2dtoList(List<Merchant> merchants);
public static void main(String[] args) {
//将dto转成entity
Merchant merchant = new Merchant();
merchant.setUsername("测试");
merchant.setMobile("123456");
MerchantDTO merchantDTO = MerchantConvert.INSTANCE.entity2dto(merchant);
System.out.println(merchantDTO);
//将entity转成dto
merchantDTO.setMerchantName("商户名称");
Merchant merchant1 = MerchantConvert.INSTANCE.dto2entity(merchantDTO);
System.out.println(merchant1);
//定义list
ArrayList arrayList = new ArrayList();
arrayList.add(merchant);
//将list转换成包含dto的list
List list = MerchantConvert.INSTANCE.entityList2dtoList(arrayList);
System.out.println(list);
}
}