基本介绍
What?
MapStruct是一个代码生成器,它基于约定优于配置的方法大大简化了Java Bean类型之间的映射的实现。
生成的映射代码使用简单的方法调用,因此速度快,类型安全且易于理解。
Why?
多层应用程序通常需要在不同的对象模型(例如实体和DTO)之间进行映射。编写此类映射代码是一项繁琐且容易出错的任务。 MapStruct旨在通过使其尽可能自动化来简化这项工作。
与其他映射框架相比,MapStruct在编译时生成Bean映射,以确保高性能,允许快速的开发人员反馈和彻底的错误检查。
How?
MapStruct是一种注释处理器,已插入Java编译器,可用于命令行构建(Maven,Gradle等),也可用于您首选的IDE中。
MapStruct使用合理的默认值,但是在配置或实现特殊行为时会避开您。
项目和代码
我们使用 idea 开发工具创建一个 SpringBoot 项目,添加相应的依赖,pom.xml
配置文件如下所示
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<org.mapstruct.version>1.3.1.Final</org.mapstruct.version>
<org.projectlombok.version>1.18.0</org.projectlombok.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.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>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
配置文件 application.yml
server:
port: 9999
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/demo?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC
username: root
password: 123456
jpa:
show-sql: true
open-in-view: false
hibernate:
ddl-auto: update
properties:
hibernate:
format_sql: true
实体类
@Entity
@Data
public class Car {
@Id
@GeneratedValue
private Integer id;
private String make;
private int numberOfSeats;
private CarType type;
public Car(String make, int numberOfSeats, CarType type) {
this.make = make;
this.numberOfSeats = numberOfSeats;
this.type = type;
}
}
枚举
public enum CarType {
SEDAN
}
DTO
@Data
public class CarDto {
private String make;
private int seatCount;
private String type;
}
mapper
@Mapper
public interface CarMapper {
CarMapper INSTANCE = Mappers.getMapper(CarMapper.class);
@Mapping(source = "numberOfSeats", target = "seatCount")
CarDto carToCarDto(Car car);
}
测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class CarMapperTest {
@Test
public void shouldMapCarToDto() {
//given
Car car = new Car("Morris", 5, CarType.SEDAN);
//when
CarDto carDto = CarMapper.INSTANCE.carToCarDto(car);
System.out.println(carDto);
}
}