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