Java 使用 MapStruct 转换对象

Java MapStruct About 4,140 words

介绍

MapStruct的作用是将类A转换为类B,省去了转换的settergetter编码。

插件

IDEA可安装MapStruct插件。

添加依赖

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>1.4.2.Final</version>
</dependency>

添加插件

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>11</source>
                <target>11</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>1.4.2.Final</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

配合Lombok时需添加插件lombok

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <source>11</source>
        <target>11</target>
        <annotationProcessorPaths>
            <path>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.22</version>
            </path>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>1.4.2.Final</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

映射关系

类 A

@Data
public class User {

    private String name;

    private int age;

    private String[] hobby;

    private int gender;

}

类 B

@Data
public class UserDto {

    private String name;

    private int age;

    private String[] hobby;

    private String sex;

    private String degree;

}

相同字段映射

User是需要被转换的类。

UserDto是转换后的类。

如果都是相同字段,则无需额外操作。

import org.mapstruct.Mapper;

@Mapper
public interface UserMapper {

    UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

    UserDto toDto(User user);

}

不同字段映射

User中的gender字段对应于UserDto类的sex

@Mapper
public interface UserMapper {

    UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

    @Mapping(source = "user.gender", target = "sex")
    UserDto toDto1(User user);

}

多个类

C

@Data
public class Education {

    private String degreeName;

}

Education中的degreeName映射到UserDto中的degree

@Mapper
public interface UserMapper {

    UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

    @Mapping(source = "education.degreeName", target = "degree")
    UserDto toDto2(User user, Education education);

}

子对象映射

@Data
public class User {

    private List<Address> addressList;

}

@Data
public class UserDto {

    private List<AddressDto> addressDtoList;

}

创建子对象映射类

@Mapper
public interface AddressMapper {

    AddressMapper INSTANCE = Mappers.getMapper(AddressMapper.class);

    AddressDto toDto(Address address);

}

@Mapper注解中使用uses参数

@Mapper(uses = AddressMapper.class)
public interface UserMapper {

    UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

    @Mapping(source = "user.addressList", target = "addressDtoList")
    UserDto toDto3(User user);

}

映射日期

实体类

@Data
public class User {

    private LocalDateTime birthday;

}

@Data
public class UserDto {

    private String birthdayStr;

}

Mapper

@Mapper
public interface UserMapper {

    UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

    @Mapping(source = "birthday", target = "birthdayStr", dateFormat = "dd/MMM/yyyy")
    UserDto toDto4(User user);

}

参考

https://zhuanlan.zhihu.com/p/368731266

官网

https://mapstruct.org

Views: 1,219 · Posted: 2022-06-05

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh