MapStruct 爬坑入門


  1. 目錄

    1. pom配置

    2. 基本使用

    3. 結合lombok使用

    4. mapStruct解析

    5. 參考資料

    pom配置

    第一步當然是引入pom依賴,目前1.3版本還是beta所以選擇引入1.2版本,使用IDEA的小伙伴推薦去插件商店搜索MapStruct,下載插件可以獲得更好的體驗

     

    <properties>
        <org.mapstruct.version>1.2.0.Final</org.mapstruct.version>
    </properties>
    
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-jdk8</artifactId>
        <version>${org.mapstruct.version}</version>
    </dependency>
    <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.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>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>

     

  2. 基本使用

    省略了getters, setters 以及構造方法,自行添加

    public class CarDto {
     
        private String make;
        private int seatCount;
        private String type;
     
        //constructor, getters, setters ...
    }
    public class Car {
    ​
        private String make;
        private int numberOfSeats;
    }
    @Mapper
    public interface CarMapper {
     
        CarMapper INSTANCE = Mappers.getMapper( CarMapper.class );
     
        @Mapping(source = "numberOfSeats", target = "seatCount")
        CarDto carToCarDto(Car car);
    }
    test
    
    public static void main(String[] args) {
            Car car = new Car( "Morris", 120 );
            //轉換對象
            CarDto carDto = CarMapper.INSTANCE.carToCarDto( car );
        
            //測試
            System.out.println( carDto );
            System.out.println( carDto.getMake() );
            System.out.println( carDto.getSeatCount() );
            System.out.println(carDto.getType());
        }

     

    至此一個簡單的demo就已經完成了,但是項目中會用到lombok插件,無法直接使用,因此開始對pom進行改造

    結合lombok使用

    修改pom依賴

    注意防坑,這里maven插件要使用3.6.0版本以上、lombok使用1.16.16版本以上,不然會遇到感人的報錯,除此之外沒有寫 getters, setters也會出現這個報錯

    Error:(12, 5) java: No property named "numberOfSeats" exists in source parameter(s). Did you mean "null"?

    <plugins>
       <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.6.0</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>1.16.10</version>
                       </path>  
                  </annotationProcessorPaths>
              </configuration>
        </plugin>
    </plugins>

     

    完工,重復以上測試代碼完美通過。至此完成,

    mapStruct解析

    有的小伙伴要問了這個mapStruct比modelmapper使用起來復雜多了,為什么用這個呢?答案是這個在編譯期生成的代碼,查看class文件,發現CarDto carToCarDto(Car car);這個方法的實現是在代碼編譯后就生成了,modelmapper則是基於反射的原理,速度自然不能比,要興趣的小伙伴可以轉去另一篇性能對比的文章查看

     

    參考資料

    官方文檔

    https://blog.csdn.net/paincupid/article/details/71247255


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM