mapstruct基于注解驱动的bean 转换工具
mapstruct 是一个不错的进行bean 转换处理的工具,使用简单,同时也比较高效,以下是一个简单的学习
环境准备
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dalong</groupId>
<artifactId>mapstruct-learning</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<encoding>UTF-8</encoding>
<java.version>1.8</java.version>
<org.mapstruct.version>1.3.1.Final</org.mapstruct.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
</dependencies>
<build>
<finalName>myinstance-app</finalName>
<plugins>
<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>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.dalong.Application</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- 代码结构
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── dalong
│ │ ├── Application.java
│ │ ├── Car.java
│ │ ├── CarDto.java
│ │ ├── CarMapper.java
│ │ └── CarType.java
│ └── resources
└── test
└── java
- 代码说明
CarMapper 是基于mapstruct的映射,类似mybatis 的使用方法,但是mapstruct是基于注解处理自动生成代码的
CarMapper.java
package com.dalong;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
/**
@author dalong
*/
@Mapper
public interface CarMapper {
CarMapper INSTANCE = Mappers.getMapper( CarMapper.class );
@Mapping(source = "numberOfSeats", target = "seatCount")
// car type 的type 字段映射到CarDto 的type
@Mapping(source = "type.type", target = "type")
CarDto carToCarDto(Car car);
}
Car.java
package com.dalong;
/**
@author dalong
*/
public class Car {
private String make;
private int numberOfSeats;
@Override
public String toString() {
return "Car{" +
"make='" + make + '\'' +
", numberOfSeats=" + numberOfSeats +
", type=" + type +
'}';
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public int getNumberOfSeats() {
return numberOfSeats;
}
public void setNumberOfSeats(int numberOfSeats) {
this.numberOfSeats = numberOfSeats;
}
public CarType getType() {
return type;
}
public void setType(CarType type) {
this.type = type;
}
private CarType type;
}
CarDto.java
package com.dalong;
/**
@author dalong
*/
public class CarDto {
private String make;
@Override
public String toString() {
return "CarDto{" +
"make='" + make + '\'' +
", seatCount=" + seatCount +
", type='" + type + '\'' +
'}';
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public int getSeatCount() {
return seatCount;
}
public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
private int seatCount;
private String type;
}
Application.java
package com.dalong;
/**
@author dalong
*/
public class Application {
public static void main(String[] args) {
Car myCar = new Car();
myCar.setMake("ddd");
myCar.setNumberOfSeats(33);
CarType type =new CarType();
type.setType("deomapp");
myCar.setType(type);
CarDto carDto = CarMapper.INSTANCE.carToCarDto(myCar);
System.out.println(carDto);
}
}
- 效果
说明
以上是一个简单的使用,实际中我们可能需要处理集合,数据类型转换。。。,mapstruct 是一个不错的选择工具,orika对于复杂的处理也是一个
不错的选择
参考资料
https://github.com/mapstruct/mapstruct
https://mapstruct.org/
http://orika-mapper.github.io/orika-docs/intro.html