Springboot集成mapstruct

一、什么是mapstruct

MapStruct是一个代码生成器的工具类,简化了不同的Java Bean之间映射的处理,所以映射指的就是从一个实体变化成一个实体。在实际项目中,我们经常会将PO转DTO、DTO转PO等一些实体间的转换。在转换时大部分属性都是相同的,只有少部分的不同,这时我们可以通过mapStruct的一些注解来匹配不同属性,可以让不同实体之间的转换变的简单。
MapStruct官网地址: http://mapstruct.org/

二、添加依赖



<dependency>
    <groupId>org.mapstructgroupId>
    <artifactId>mapstruct-jdk8artifactId>
    <version>1.0.0.Finalversion>
dependency>

<dependency>
    <groupId>org.mapstructgroupId>
    <artifactId>mapstruct-processorartifactId>
    <version>1.0.0.Finalversion>
dependency>

三、mapstruct实体间的转换

//Info.java
public class Info{
    private Integer userId;
    private String email;
    private Integer score;
    private String remark;
    //constructor, getters, setters etc.
}
//InfoDto.java
public class InfoDto{
    private Integer id;
    private Integer value;
    private String remark;
    private User user;
    //constructor, getters, setters etc.
}
//User.java
public class User{
    private Integer userId;
    private String email;
    //constructor, getters, setters etc.
}

四、mapper接口

要生成一个PeopleDTO与PeopleEntity对象相互转换的映射器,我们需要定义一个mapper接口。像这两个实体类有些属性不一样时,我们可以通过@Mapping注解来进行转换.

  • @Mapper注解标记这个接口作为一个映射接口,并且是编译时MapStruct处理器的入口。
  • @Mapping解决源对象和目标对象中,属性名字不同的情况。
  • Mappers.getMapper自动生成的接口的实现可以通过Mapper的class对象获取,从而让客户端可以访问Mapper接口的实现。
@Mapper
public interface InfoMapper {
    PeopleMapper INSTANCE = Mappers.getMapper(InfoMapper.class);

    /**
     * PO转DTO
     *
     * @param info PO
     * @return DTO
     */
    @Mapping(target = "value", source = "score")
    @Mapping(target = "user.userId", source = "userId")
    @Mapping(target = "user.email", source = "email")
    InfoDTO entityToDTO(Info info);

    /**
     * DTO转PO
     *
     * @param infoDTO DTO
     * @param entity    PO
     */
    @Mapping(target = "score", source = "value")
    @Mapping(target = "userId", source = "user.userId")
    @Mapping(target = "email", source = "user.email")
    void updateInfoFromDto(InfoDTO InfoDTO, @MappingTarget Info info);
}

当运行是将会自动编译我们的InfoMapper.java

下面是我在学习时写的Mapper文件以及编译后的一个文件

@Mapper(componentModel = "spring")
public interface TeamMapper {

    TeamVo poToTeamVo(TeamPo teamPo);

    Team poToTeamDo(TeamPo teamPo);

    List<TeamVo> poListToVoList(List<TeamPo> teamPoList);
}
@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2019-08-08T13:30:54+0800",
    comments = "version: 1.0.0.Final, compiler: javac, environment: Java 1.8.0_211 (Oracle Corporation)"
)
@Component
public class TeamMapperImpl implements TeamMapper {

    @Override
    public TeamVo poToTeamVo(TeamPo teamPo) {
        if ( teamPo == null ) {
            return null;
        }

        TeamVo teamVo = new TeamVo();

        teamVo.setId( teamPo.getId() );
        teamVo.setName( teamPo.getName() );
        teamVo.setDepartmentId( teamPo.getDepartmentId() );
        teamVo.setDepartmentName( teamPo.getDepartmentName() );

        return teamVo;
    }

    @Override
    public Team poToTeamDo(TeamPo teamPo) {
        if ( teamPo == null ) {
            return null;
        }

        Team team = new Team();

        team.setId( teamPo.getId() );
        team.setName( teamPo.getName() );
        team.setDepartmentId( teamPo.getDepartmentId() );

        return team;
    }

    @Override
    public List<TeamVo> poListToVoList(List<TeamPo> teamPoList) {
        if ( teamPoList == null ) {
            return null;
        }

        List<TeamVo> list = new ArrayList<TeamVo>();
        for ( TeamPo teamPo : teamPoList ) {
            list.add( poToTeamVo( teamPo ) );
        }

        return list;
    }
}

五、注意

1、 由于编译可能不及时的原因,所以一开始遇到了修改了po或者dto的一个类,但是mapper没有及时的重新编译,所以dto,po,vo有更改的话,建议先clean一下之后重新编译然后运行

2、 在使用mapstruct + lombok时要注意maven-comiler-plugin插件版本一定要在3.6.0以上,若版本低,则会报找不到属性的错误
下面是一个依赖模板


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.1.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>xyz.suiwogroupId>
    <artifactId>gradingdogartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>gradingdogname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
        <org.mapstruct.version>1.2.0.Beta2org.mapstruct.version>
        <org.projectlombok.version>1.16.14org.projectlombok.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.0.1version>
        dependency>

        
        <dependency>
            <groupId>org.mapstructgroupId>
            <artifactId>mapstructartifactId> 
            <version>${org.mapstruct.version}version>
        dependency>

        <dependency>
            <groupId>org.mapstructgroupId>
            <artifactId>mapstruct-jdk8artifactId>
            <version>${org.mapstruct.version}version>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>${org.projectlombok.version}version>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.6.0version>
                <configuration>
                    <source>${java.version}source> 
                    <target>${java.version}target> 
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombokgroupId>
                            <artifactId>lombokartifactId>
                            <version>${org.projectlombok.version}version>
                        path>
                        <path>
                            <groupId>org.mapstructgroupId>
                            <artifactId>mapstruct-processorartifactId>
                            <version>${org.mapstruct.version}version>
                        path>
                    annotationProcessorPaths>
                    <compilerArgs>
                        <arg>-Amapstruct.suppressGeneratorTimestamp=truearg>
                        <arg>-Amapstruct.defaultComponentModel=springarg>
                    compilerArgs>
                configuration>
            plugin>
        plugins>
    build>
project>

你可能感兴趣的:(Java,SpringBoot,Java,Springboot,mapstruct)