1、mapstruct和其他映射工具的對比請參考以下地址
https://www.cnblogs.com/javaguide/p/11861749.html
2、pom.xml文件中添加依賴,在使用lombok的時候mapstruct插件會不生效,引入mapstruct-jdk8來解決問題
<dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>1.3.0.Final</version> </dependency> <!-- 解決springboot 項目使用 lombok 插件后,添加 mapstruct 插件,maven 編譯時 mapstruct 插件不起作用 --> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-jdk8</artifactId> <version>1.3.0.Final</version> </dependency>
3、添加插件
<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>1.3.0.Final</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins>
4、idea安裝插件
5、代碼編寫
1)舉例car實體
public class Car { private String make; private int numberOfSeats; private CarType type; }
2)舉例cardto
public class CarDto { private String make; private int seatCount; private String type; }
3)舉例mapper
@Mapper public interface CarMapping { CarMapping INSTANCE = Mappers.getMapper( CarMapping .class ); @Mapping(source = "numberOfSeats", target = "seatCount") CarDto carToCarDto(Car car); }
4)使用
CarDto carDto=CarMapping.INSTANCE.carToCarDto(car);
6、高級使用參考
https://blog.csdn.net/gaochuanlove/article/details/101061474