JDK11使用MapStruct

背景

​ 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);

    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值