MapStruct学习笔记

jupiter
2023-10-12 / 0 评论 / 40 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2023年10月12日,已超过197天没有更新,若内容或图片失效,请留言反馈。

1.MapStruct介绍

在现在多模块多层级的项目中,应用于应用之间,模块于模块之间数据模型一般都不通用,每层都有自己的数据模型。这种对象与对象之间的互相转换,目前都是使用get,set方法,或者使用自定义的Beans.copyProperties进行转换。使用get,set方式会使得编码非常的麻烦,BeanUtils.copyProperties的方式是使用反射的方式,对性能的消耗比较大。

Mapstruct的性能远远高于BeanUtils,

对象转换次数属性个数BeanUtils耗时Mapstruct耗时
5千万次614秒1秒
5千万次1536秒1秒
5千万次2555秒1秒

MapStruct是一个开源的基于Java的代码生成器,用于创建实现Java Bean之间转换的扩展映射器。使用MapStruct,我们只需要创建接口,而该库会通过注解在编译过程中自动创建具体的映射实现,大大减少了通常需要手工编写的样板代码的数量。

2.maven项目使用MapStruct

2.1 pom依赖

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>1.5.0.Final</version>
</dependency>
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <version>1.5.0.Final</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
</dependency>

2.2 配置打包插件

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>17</source>
                <target>17</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>1.5.0.Final</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

2.3 创建实体类

  • Source .java
@Data
public class Source  {
    private Long id;
    private Date gmtCreate;
    private Date createTime;
    private Long buyerId;
    private Long age;
    private String userNick;
    private String userVerified;
}
  • Target.java
@Data
public class `Target  {
    private Long id;
    private Date gmtCreate;
    private Date createTime;
    private Long buyerId;
    private Long age;
    private String userNick;
    private String userVerified;
}

2.4 创建映射接口

定义一个接口,其中包含源类和目标类之间的映射方法。MapStruct将在编译时自动为这个接口生成实现

import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

@Mapper
public interface SourceTargetMapper  {
    SourceTargetMapper INSTANCE = Mappers.getMapper(SourceTargetMapper.class);
    Target sourceToTarget(Source source);
}

2.5 测试映射接口

@Test
public void testMapStruct(){
        Source source = new Source();
        source.setId(1L);
        source.setGmtCreate(new Date());
        source.setCreateTime(new Date());
        source.setBuyerId(43252534643L);
        source.setAge(99L);
        source.setUserNick("mapstruct测试");
        source.setUserVerified("ok");
        System.out.println(source);
        Target target = SourceTargetMapper.INSTANCE.sourceToTarget(source);
        System.out.println(target);
}
  • 运行结果
Source{id=1, gmtCreate=Wed Oct 11 15:46:15 CST 2023, createTime=Wed Oct 11 15:46:15 CST 2023, buyerId=43252534643, age=99, userNick='mapstruct测试', userVerified='ok'}
Target{id=1, gmtCreate=Wed Oct 11 15:46:15 CST 2023, createTime=Wed Oct 11 15:46:15 CST 2023, buyerId=43252534643, age=99, userNick='mapstruct测试', userVerified='ok'}

3. @Mapping解决字段名不一致的问题

@Mapping(target = "targetName", source = "sourceName"),此处的意思就是在转化的过程中,将Source的Target属性值赋值给sourceName的targetName属性。

@Data
public class Source {  
    private String sourceName;  
}  

@Data
public class Target {  
    private String targetName;  
}
import org.mapstruct.Mapper;  
import org.mapstruct.Mapping;  
  
@Mapper  
public interface SourceTargetMapper {  
    @Mapping(target = "targetName", source = "sourceName")
    Target sourceToTarget(Source source);  
}

4.String转日期&String转数字&忽略某个字端&给默认值等

@Mapping(target = "createTime", source = "createTime", dateFormat = "yyyy-MM-dd")
@Mapping(target = "age", source = "age", numberFormat = "#0.00")
@Mapping(target = "id", ignore = true)
@Mapping(target = "userVerified", defaultValue = "defaultValue-2")

参考资料

  1. MapStruct使用指南 - 知乎 (zhihu.com)
  2. 告别BeanUtils,Mapstruct从入门到精通 - 掘金 (juejin.cn)
  3. Maven中配置maven-compiler-plugin 插件和jdk 17版本 - 楼兰胡杨 - 博客园 (cnblogs.com)
0

评论 (0)

打卡
取消