0%

Springboot集成MapStruct


一、什么是mapstruct

MapStruct是一个代码生成器的工具类,简化了不同的Java Bean之间映射的处理,所以映射指的就是从一个实体变化成一个实体。在实际项目中,我们经常会将PO转DTO、DTO转PO等一些实体间的转换。在转换时大部分属性都是相同的,只有少部分的不同,这时我们可以通过mapStruct的一些注解来匹配不同属性,可以让不同实体之间的转换变的简单。
MapStruct官网地址: http://mapstruct.org/


二、添加依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
<!--MapStruct依赖-->
<!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct-jdk8 -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.0.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct-processor -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.0.0.Final</version>
</dependency>

三、mapstruct实体间的转换

1
2
3
4
5
6
7
8
//Info.java
public class Info{
private Integer userId;
private String email;
private Integer score;
private String remark;
//constructor, getters, setters etc.
}
1
2
3
4
5
6
7
8
//InfoDto.java
public class InfoDto{
private Integer id;
private Integer value;
private String remark;
private User user;
//constructor, getters, setters etc.
}
1
2
3
4
5
6
//User.java
public class User{
private Integer userId;
private String email;
//constructor, getters, setters etc.
}

四、mapper接口

要生成一个PeopleDTO与PeopleEntity对象相互转换的映射器,我们需要定义一个mapper接口。像这两个实体类有些属性不一样时,我们可以通过@Mapping注解来进行转换.

  • @Mapper注解标记这个接口作为一个映射接口,并且是编译时MapStruct处理器的入口。
  • @Mapping解决源对象和目标对象中,属性名字不同的情况。
  • Mappers.getMapper自动生成的接口的实现可以通过Mapper的class对象获取,从而让客户端可以访问Mapper接口的实现。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@Mapper
public interface InfoMapper {
PeopleMapper INSTANCE = Mappers.getMapper(InfoMapper.class);

/**
* PO转DTO
*
* @param info PO
* @return DTO
*/
@Mapping(target = "value", source = "score")
@Mapping(target = "user.userId", source = "userId")
@Mapping(target = "user.email", source = "email")
InfoDTO entityToDTO(Info info);

/**
* DTO转PO
*
* @param infoDTO DTO
* @param entity PO
*/
@Mapping(target = "score", source = "value")
@Mapping(target = "userId", source = "user.userId")
@Mapping(target = "email", source = "user.email")
void updateInfoFromDto(InfoDTO InfoDTO, @MappingTarget Info info);
}

当运行是将会自动编译我们的InfoMapper.java

下面是我在学习时写的Mapper文件以及编译后的一个文件

1
2
3
4
5
6
7
8
9
@Mapper(componentModel = "spring")
public interface TeamMapper {

TeamVo poToTeamVo(TeamPo teamPo);

Team poToTeamDo(TeamPo teamPo);

List<TeamVo> poListToVoList(List<TeamPo> teamPoList);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2019-08-08T13:30:54+0800",
comments = "version: 1.0.0.Final, compiler: javac, environment: Java 1.8.0_211 (Oracle Corporation)"
)
@Component
public class TeamMapperImpl implements TeamMapper {

@Override
public TeamVo poToTeamVo(TeamPo teamPo) {
if ( teamPo == null ) {
return null;
}

TeamVo teamVo = new TeamVo();

teamVo.setId( teamPo.getId() );
teamVo.setName( teamPo.getName() );
teamVo.setDepartmentId( teamPo.getDepartmentId() );
teamVo.setDepartmentName( teamPo.getDepartmentName() );

return teamVo;
}

@Override
public Team poToTeamDo(TeamPo teamPo) {
if ( teamPo == null ) {
return null;
}

Team team = new Team();

team.setId( teamPo.getId() );
team.setName( teamPo.getName() );
team.setDepartmentId( teamPo.getDepartmentId() );

return team;
}

@Override
public List<TeamVo> poListToVoList(List<TeamPo> teamPoList) {
if ( teamPoList == null ) {
return null;
}

List<TeamVo> list = new ArrayList<TeamVo>();
for ( TeamPo teamPo : teamPoList ) {
list.add( poToTeamVo( teamPo ) );
}

return list;
}
}

五、注意

1、 由于编译可能不及时的原因,所以一开始遇到了修改了po或者dto的一个类,但是mapper没有及时的重新编译,所以dto,po,vo有更改的话,建议先clean一下之后重新编译然后运行

2、 在使用mapstruct + lombok时要注意maven-comiler-plugin插件版本一定要在3.6.0以上,若版本低,则会报找不到属性的错误
下面是一个依赖模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>xyz.suiwo</groupId>
<artifactId>gradingdog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gradingdog</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<org.mapstruct.version>1.2.0.Beta2</org.mapstruct.version>
<org.projectlombok.version>1.16.14</org.projectlombok.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>

<!--MapStruct依赖-->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId> <!-- use mapstruct-jdk8 for Java 8 or higher -->
<version>${org.mapstruct.version}</version>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</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.6.0</version>
<configuration>
<source>${java.version}</source> <!-- or higher, depending on your project -->
<target>${java.version}</target> <!-- or higher, depending on your project -->
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-Amapstruct.suppressGeneratorTimestamp=true</arg>
<arg>-Amapstruct.defaultComponentModel=spring</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>