栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

别再用 BeanUtils 了,这款 PO VO DTO 转换神器不香么?

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

别再用 BeanUtils 了,这款 PO VO DTO 转换神器不香么?

今日推荐

借助Redis锁,完美解决高并发秒杀问题

还在直接用JWT做鉴权?JJWT真香

Spring Boot 操作 Redis 的各种实现

Fluent Mybatis 牛逼!

Nginx 常用配置清单

这玩意比ThreadLocal叼多了,吓得我赶紧分享出来。

来源:toutiao.com/i6891531055631696395

老铁们是不是经常为写一些实体转换的原始代码感到头疼,尤其是实体字段特别多的时候。介绍一个开源项目 mapstruct ,可以轻松优雅的进行转换,简化你的代码。

当然有的人喜欢写get set,或者用BeanUtils 进行复制,代码只是工具,本文只是提供一种思路。

先贴下官网地址吧:https://mapstruct.org/

废话不多说,上代码:

pom 配置:

UTF-8

1.8

1.8

1.4.1.Final

1.18.12

org.mapstruct

mapstruct

${org.mapstruct.version}

org.projectlombok

lombok

${org.projectlombok.version}

provided

org.mapstruct

mapstruct-processor

${org.mapstruct.version}

provided

org.apache.maven.plugins

maven-compiler-plugin

3.8.1

1.8

1.8

org.projectlombok

lombok

${org.projectlombok.version}

org.mapstruct

mapstruct-processor

${org.mapstruct.version}

关于lombok和mapstruct的版本兼容问题多说几句,maven插件要使用3.6.0版本以上、lombok使用1.16.16版本以上,另外编译的lombok mapstruct的插件不要忘了加上。否则会出现下面的错误:No property named "aaa" exists in source parameter(s). Did you mean "null"?

这种异常就是lombok编译异常导致缺少get setter方法造成的。还有就是缺少构造函数也会抛异常。学习资料:Java进阶视频资源

@Data

@Builder

@AllArgsConstructor

@NoArgsConstructor

public class Student {

private String name;

private int age;

private GenderEnum gender;

private Double height;

private Date birthday;

}

public enum GenderEnum {

Male(“1”, “男”),

Female(“0”, “女”);

private String code;

private String name;

public String getCode() {

return this.code;

}

public String getName() {

return this.name;

}

GenderEnum(String code, String name) {

this.code = code;

this.name = name;

}

}

@Data

@Builder

@AllArgsConstructor

@NoArgsConstructor

public class StudentVO {

private String name;

private int age;

private String gender;

private Double height;

private String birthday;

}

@Mapper

public interface Stude

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

ntMapper {

StudentMapper INSTANCE = Mappers.getMapper(StudentMapper.class);

@Mapping(source = “gender.name”, target = “gender”)

@Mapping(source = “birthday”, target = “birthday”, dateFormat = “yyyy-MM-dd HH:mm:ss”)

StudentVO student2StudentVO(Student student);

}

实体类是开发过程少不了的,就算是用工具生成肯定也是要有的,需要手写的部分就是这个Mapper的接口,编译完成后会自动生成相应的实现类

然后就可以直接用mapper进行实体的转换了

public class Test {

public static void main(String[] args) {

Student student = Student.builder().name(“小明”).age(6).gender(GenderEnum.Male).height(121.1).birthday(new Date()).build();

System.out.println(student);

//这行代码便是实际要用的代码

StudentVO studentVO = StudentMapper.INSTANCE.student2StudentVO(student);

System.out.println(studentVO);

}

}

mapper可以进行字段映射,改变字段类型,指定格式化的方式,包括一些日期的默认处理。

可以手动指定格式化的方法:

@Mapper

public interface StudentMapper {

StudentMapper INSTANCE = Mappers.getMapper(StudentMapper.class);

@Mapping(source = “gender”, target = “gender”)

@Mapping(source = “birthday”, target = “birthday”, dateFormat = “yyyy-MM-dd HH:mm:ss”)

StudentVO student2StudentVO(Student student);

default String getGenderName(GenderEnum gender) {

return gender.getName();

}

}

上面只是最简单的实体映射处理,下面介绍一些高级用法

1.List 转换

属性映射基于上面的mapping配置

@Mapper

public interface StudentMapper {

StudentMapper INSTANCE = Mappers.getMapper(StudentMapper.class);

@Mapping(source = “gender.name”, target = “gender”)

@Mapping(source = “birthday”, target = “birthday”, dateFormat = “yyyy-MM-dd HH:mm:ss”)

StudentVO student2StudentVO(Student student);

List students2StudentVOs(List studentList);

}

public static void main(String[] args) {

Student student = Student.builder().name(“小明”).age(6).gender(GenderEnum.Male).height(121.1).birthday(new Date()).build();

List list = new ArrayList<>();

list.add(student);

List result = StudentMapper.INSTANCE.students2StudentVOs(list);

System.out.println(result);

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/643245.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号