Bean拷贝工具MapStruct

MapStruct是一种在编译时就生产bean拷贝代码的bean copy工具。

简单实用

maven配置

这里需要注意的是插件中lombok和mapstruct-processor的配置

    <properties>
        <org.mapstruct.version>1.3.1.Final</org.mapstruct.version>
        <org.projectlombok.version>1.18.12</org.projectlombok.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${org.projectlombok.version}</version>
            <scope>provided</scope>
        </dependency>
    
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${org.projectlombok.version}</version>
                        </path>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

配置转换器

package com.yihengyi.beans.converters;

import com.yihengyi.beans.dos.PersonDo;
import com.yihengyi.beans.dtos.PersonDto;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

@Mapper
public interface PersonConverter {
    PersonConverter INSTANCE = Mappers.getMapper(PersonConverter.class);

    @Mappings(@Mapping(source = "name", target = "userName"))
    PersonDto do2dto(PersonDo person);
}

测试

package com.yihengyi.mapstruct;

import com.yihengyi.beans.converters.PersonConverter;
import com.yihengyi.beans.dos.PersonDo;
import com.yihengyi.beans.dtos.PersonDto;
import com.yihengyi.beans.enums.Gender;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

import java.util.Date;

public class BeanCopyTest {
    @Test
    public void testBeanCopy() {
        PersonDo personDo = new PersonDo();
        personDo.setName("Hollis");
        personDo.setAge(28);
        personDo.setBirthday(new Date());
        personDo.setId(1);
        personDo.setGender(Gender.FEMALE);

        PersonDto personDto = PersonConverter.INSTANCE.do2dto(personDo);
        System.out.println(personDto);
        assertEquals("Hollis", personDto.getUserName());
    }
}

测试结果:
运行mvn test命令

Running com.yihengyi.mapstruct.BeanCopyTest
PersonDto(userName=Hollis, age=28, birthday=Sat Aug 15 23:15:53 CST 2020, gender=FEMALE)
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.066 sec

代码地址

https://github.com/yihengyi/map-struct-test

参考:

https://mp.weixin.qq.com/s/a-UDuXallZkHqj5hJSukZg

原文地址:https://www.cnblogs.com/liuchengcc/p/13510393.html