Eswlnk Blog Eswlnk Blog
  • 资源
    • 精彩视频
    • 破解专区
      • WHMCS
      • WordPress主题
      • WordPress插件
    • 其他分享
    • 极惠VPS
    • PDF资源
  • 关于我
    • 论文阅读
    • 关于本站
    • 通知
    • 左邻右舍
    • 玩物志趣
    • 日志
    • 专题
  • 热议话题
    • 游戏资讯
  • 红黑
    • 渗透分析
    • 攻防对抗
    • 代码发布
  • 自主研发
    • 知识库
    • 插件
      • ToolBox
      • HotSpot AI 热点创作
    • 区块
    • 快乐屋
    • 卡密
  • 乱步
    • 文章榜单
    • 热门标签
  • 问答中心反馈
  • 注册
  • 登录
首页 › 代码发布 › 「代码发布」MapStruct 拷贝类属性

「代码发布」MapStruct 拷贝类属性

Eswlnk的头像
Eswlnk
2022-08-19 0:37:22
「代码发布」MapStruct 拷贝类属性-Eswlnk Blog
智能摘要 AI
本文讨论了如何将`StudentVo`对象的属性值拷贝到`StudentDto`对象中,并提出了三种解决方案:传统手动拷贝、使用Spring的`BeanUtils.copyProperties`方法以及推荐使用的`MapStruct`。 1. **传统方法**:通过`getter/setter`方法逐一赋值,适用于属性较少的情况。 2. **BeanUtils**:简化代码,但使用反射影响性能,且需属性名一致。 3. **MapStruct**:推荐方案,自动在编译期生成高效、类型安全的映射代码,支持自定义映射规则,且无需依赖反射,支持复杂对象和列表的转换。 `MapStruct`不仅提高了代码可读性和维护性,还显著提升了性能,特别适合大规模项目中复杂的对象转换需求。

背景

我们有这样一个场景,有一个StudentDto类,还有一个StudentVo类

@Data
public class StudentDto {
    private String id;
    private String code;
    private String sex;
    private String userName;
}
@Data
public class StudentVo {
    private String id;
    private String name;
    private String code;
    private String age;
    private String score;
    private String sex;
}

问题

如果我们知道StudentVo的值,需要将StudentVo的属性拷贝到StudentDto中,你会怎么做。

StudentVo的值如下

public StudentVo initVo() {
    StudentVo studentVo = new StudentVo();
    studentVo.setId("1");
    studentVo.setAge("27");
    studentVo.setName("Lvshen");
    studentVo.setCode("001");
    studentVo.setScore("100");
    studentVo.setSex("male");
    return studentVo;
}

解决一

传统的解决方法,通过getter/setter方法将对应的属性值进行拷贝。

@org.junit.Test
public void test3() {
    StudentVo studentVo = initVo();
    StudentDto studentDto = new StudentDto();
    studentDto.setCode(studentVo.getCode());
    studentDto.setId(studentVo.getId());
    studentDto.setSex(studentVo.getSex());
    studentDto.setUserName(studentVo.getName());

    System.out.println(studentDto);
}

测试结果

「代码发布」MapStruct 拷贝类属性-Eswlnk Blog

看了上面的方法,你可能觉得不是很简单么。但如果属性非常多,比如有20多个。用上面的方法就会不美观,满屏的getter/setter方法,看着都眼花。

解决二

这时我们就可以使用BeanUtils.copyProperties方法啦,这里的BeanUtils是Spring的,而不是apache的。

@org.junit.Test
public void test2() {
    StudentVo studentVo = initVo();
    StudentDto studentDto = new StudentDto();
    BeanUtils.copyProperties(studentVo,studentDto);

    System.out.println(studentDto);
}

测试结果

「代码发布」MapStruct 拷贝类属性-Eswlnk Blog

解决三(推荐)

我们还可以使用性能更优越的MapStruct,你可能没有听过这个东西。没关系,我们直接上代码。

MapStruct是一个可以生成类型安全的,高性能的且无依赖的 JavaBean 映射代码的注解处理器,可以在编译期生成对应的mapping,既没有BeanUtils等工具使用反射的性能问题,又免去了自己写映射代码的繁琐。

引入Maven依赖

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>1.3.1.Final</version>
</dependency>

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <version>1.3.1.Final</version>
</dependency>

实体拷贝

我们先编写一个StudentConverter类

@Mapper
public interface StudentConverter {
    StudentConverter INSTANCE = Mappers.getMapper(StudentConverter.class);

    @Mappings(@Mapping(source = "name",target = "userName"))
    StudentDto vo2dto(StudentVo vo);
}

这里的@Mapper来源于org.mapstruct.Mapper,用来说明这是一个实体转换类接口。

@Mappings用来声明成员属性的映射,source = "name",target = "userName"即将StudentVo中name的值拷贝给StudentDto中的userName,如果属性名称相同,就不需要做这个映射。

测试结果

@org.junit.Test
public void test1() {
    StudentVo studentVo = initVo();
    StudentDto studentDto = StudentConverter.INSTANCE.vo2dto(studentVo);

    System.out.println(studentDto);
}
「代码发布」MapStruct 拷贝类属性-Eswlnk Blog

List集合拷贝

你以为MapStruct只能进行实体之间的拷贝?NO,MapStruct还可以进行List之间的拷贝,这个就太牛了。

@Mapper
public interface StudentConverter {
    StudentConverter INSTANCE = Mappers.getMapper(StudentConverter.class);

    @Mappings(@Mapping(source = "name",target = "userName"))
    StudentDto vo2dto(StudentVo vo);

    List<StudentDto> listVo2dto(List<StudentVo> vos);
}

测试

「代码发布」MapStruct 拷贝类属性-Eswlnk Blog
public void test() {
        List<StudentVo> voList = initVoList();
        List<StudentDto> studentDtos = StudentConverter.INSTANCE.listVo2dto(voList);
        System.out.println(studentDtos);
    }

测试结果

「代码发布」MapStruct 拷贝类属性-Eswlnk Blog

当然MapStruct等功能远比你想象的要多,有兴趣的可以看看这篇文章

https://www.cnblogs.com/homejim/p/11313128.html

为什么推荐使用MapStruct

市面上 BeanUtils底层是使用反射的,我们知道使用反射会影响性能。而且BeanUtils需要类型和名称都一样才会进行映射, 但在很多时候, 由于不同的团队之间使用的名词不一样, 还是需要很多的手动使用getter/setter。

于是MapStruct诞生了。

MapSturct 是一个生成类型安全, 高性能且无依赖的 JavaBean 映射代码的注解处理器(annotation processor)。

它有下面几个特点:

  1. 基于注解的处理器
  2. 可以自定义 JavaBean 之间的属性映射
  3. 类型安全, 高性能, 无依赖性

编译之后会生成方法实现

实现的类、方法如下

「代码发布」MapStruct 拷贝类属性-Eswlnk Blog

该工具可以帮我们实现 JavaBean 之间的转换, 通过注解的方式。通过 MapStruct 来生成的代码, 其类似于人手写。速度上可以得到保证。

本站默认网盘访问密码:1166
本站默认网盘访问密码:1166
javaMapStructstring代码发布平台拷贝
0
0
Eswlnk的头像
Eswlnk
一个有点倒霉的研究牲站长
赞赏
「代码发布」k8s 网络转发问题记录
上一篇
「代码发布」CentOS环境挂载SMB实现共享文件夹
下一篇

评论 (0)

请登录以参与评论
现在登录
    发表评论

猜你喜欢

  • 「攻防对抗」利用 fastjson 原生反序列化与动态代理突破安全限制
  • 「日志记录」逆向必应翻译网页版API实现免费调用
  • 「代码分享」第三方平台VIP视频解析API接口
  • 「至臻原创」某系统网站登录功能监测
  • 「JAVA教程」Spring Boot 中使用 JSON Schema 来校验复杂JSON数据
Eswlnk的头像

Eswlnk

一个有点倒霉的研究牲站长
1108
文章
319
评论
679
获赞

随便看看

「代码分享」Vue项目配置 webpack-obfuscator 进行代码加密混淆
2022-10-20 22:27:49
将libfuzzer移植为动态链接库的实践:增强灵活性与应用场景拓展
2023-07-10 13:19:25
「教程分享」如何解决 WordPress 添加媒体按钮突然失效的问题
2022-10-23 20:46:17

文章目录

专题展示

WordPress53

工程实践37

热门标签

360 AI API CDN java linux Nginx PDF PHP python SEO Windows WordPress 云服务器 云服务器知识 代码 免费 安全 安卓 工具 开发日志 微信 微软 手机 插件 攻防 攻防对抗 教程 日志 渗透分析 源码 漏洞 电脑 破解 系统 编程 网站优化 网络 网络安全 脚本 苹果 谷歌 软件 运维 逆向
  • 首页
  • 知识库
  • 地图
Copyright © 2023-2025 Eswlnk Blog. Designed by XiaoWu.
本站CDN由 壹盾安全 提供高防CDN安全防护服务
蜀ICP备20002650号-10
页面生成用时 0.479 秒   |  SQL查询 43 次
本站勉强运行:
友情链接: Eswlnk Blog 网站渗透 倦意博客 特资啦!个人资源分享站 祭夜博客 iBAAO壹宝头条
  • WordPress142
  • 网络安全64
  • 漏洞52
  • 软件52
  • 安全48
现在登录
  • 资源
    • 精彩视频
    • 破解专区
      • WHMCS
      • WordPress主题
      • WordPress插件
    • 其他分享
    • 极惠VPS
    • PDF资源
  • 关于我
    • 论文阅读
    • 关于本站
    • 通知
    • 左邻右舍
    • 玩物志趣
    • 日志
    • 专题
  • 热议话题
    • 游戏资讯
  • 红黑
    • 渗透分析
    • 攻防对抗
    • 代码发布
  • 自主研发
    • 知识库
    • 插件
      • ToolBox
      • HotSpot AI 热点创作
    • 区块
    • 快乐屋
    • 卡密
  • 乱步
    • 文章榜单
    • 热门标签
  • 问答中心反馈