MapStruct 爬坑入门

  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>

基本使用

省略了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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,446评论 25 708
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,960评论 2 59
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,959评论 6 342
  • 看惯了身边朋友们的爱恋分分合合,毫无预料的,在某个不经意间的谈笑间,一句话,如是说:他已经不是我男朋友了。赶紧急着...
    琳琳啵琳琳阅读 178评论 0 0
  • 2018-4-19 晴 今天天气不错,早上送完孩子,回来,和朋友聚了聚,中午的时候看了一下手机,在群里发生了点...
    如今老衲已成佛阅读 167评论 1 0