MapStruct 快速入门

MapStruct

MapStruct – Java bean mappings, the easy way!

官方网站

https://mapstruct.org
https://mapstruct.org/documentation/stable/reference/html/#lombok

MapStruct可以在编译时,生成DTO和DO的映射,我们只需要定义好DTO和DO之间的映射关系即可,解放生产力。还可以和Lombok结合使用。

MapStruct + Lombok 强强联合

<properties>
    <org.mapstruct.version>1.4.2.Final</org.mapstruct.version>
    <org.projectlombok.version>1.18.16</org.projectlombok.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>${org.mapstruct.version}</version>
    </dependency>

    <!-- lombok dependency should not end up on classpath -->
    <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.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${org.mapstruct.version}</version>
                    </path>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${org.projectlombok.version}</version>
                    </path>

                    <!-- additional annotation processor required as of Lombok 1.18.16 -->
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok-mapstruct-binding</artifactId>
                        <version>0.1.0</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

MapStruct 案例及测试代码

@Getter
@Setter
public class DoctorDTO {
    private Integer id;
    private String name;
}

@Getter
@Setter
public class Doctor {
    private Integer id;
    private String name;
}

// 定义DTO与实体之间的转换
@Mapper
public interface DoctorMapping {
    DoctorMapping INSTANCE = Mappers.getMapper(DoctorMapping.class);

    DoctorDTO toDTO(Doctor doctor);
}

// 测试类
public class MapStructTest {
    @Test
    public void testToDTO() {
        Integer id = 9527;
        String name = "zxx";
        Doctor doctor = new Doctor();
        doctor.setId(id);
        doctor.setName(name);
        DoctorDTO doctorDTO = DoctorMapping.INSTANCE.toDTO(doctor);
        Assert.assertEquals(id, doctorDTO.getId());
        Assert.assertEquals(name, doctorDTO.getName());
    }
}

认识两个注解

@Mapper

标识这个文件是MapStruct映射文件,需要将该文件定义为接口或者抽象类,mapstruct插件会在编译时根据接口中的方法生成实现类,类中包含DTO和DO的转换代码。

@Mapping

@Mapping用于指定字段名不一致情况下的映射关系

@Mapper
public abstract class SourceMapping {
    public static final SourceMapping INSTANCE = Mappers.getMapper(SourceMapping.class);

    @Mapping(target = "targetName", source = "sourceName")
    public abstract Target toTarget(Source source);

}

enjoy yourself!!!

posted @ 2022-07-16 14:33  crazy-zz5536  阅读(69)  评论(0编辑  收藏  举报