遇到MapStruct后,再也不手写PO,DTO,VO对象之间的转换了

本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

转载声明:转载请注明出处,本技术博客是本人原创文章

本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

原文链接:blog.ouyangsihai.cn >> 遇到MapStruct后,再也不手写PO,DTO,VO对象之间的转换了

点击上方 **好好学java **,选择 **星标 **公众号


重磅资讯、干货,第一时间送达


今日推荐:

个人原创100W+访问量博客:点击前往,查看更多

介绍

在工作中,我们经常要进行各种对象之间的转换。

PO:persistent object 持久对象,对应数据库中的一条记录VO:view object 表现层对象,最终返回给前端的对象DTO:data transfer object数据传输对象,如dubbo服务之间传输的对象

如果这些对象的属性名相同还好,可以用如下工具类赋值

Spring BeanUtils Cglib BeanCopier 避免使用Apache BeanUtils,性能较差

如果属性名不同呢?如果是将多个PO对象合并成一个VO对象呢?好在有MapStruct神器,可以帮助我们快速转换

在pom文件中加入如下依赖即可


<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-jdk8</artifactId>
    <version>1.2.0.CR1</version>
</dependency>

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <version>1.2.0.CR1</version>
    <scope>provided</scope>
</dependency>

对象互转


@Data
@Builder
public class StudentPO {

    private Integer id;
    private String name;
    private Integer age;
    private String className;
}

@Data
public class StudentVO {

    private Integer id;
    private String studentName;
    private Integer studentAge;
    private String schoolName;
}

@Mapper
public interface StudentMapper {

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

    @Mappings({
            @Mapping(source = "name", target = "studentName"),
            @Mapping(source = "age", target = "studentAge")
    })
    StudentVO po2Vo(StudentPO studentPO);
}
  1. 新建一个Mapper接口,上面加上@Mapper注解1. 新建一个成员变量INSTANCE1. 用@Mapping注解指定映射关系,名字相同的就不用再指定了,会自动映射
    测试效果如下,名字不同且没有指定映射关系的会被设置为null

@Test
public void studentPo2Vo() {
    StudentPO studentPO = StudentPO.builder().id(10).name("test")
            .age(24).className("教室名").build();
    StudentVO studentVO = StudentMapper.INSTANCE.po2Vo(studentPO);
    // StudentVO(id=10, studentName=test, studentAge=24, schoolName=null)
    System.out.println(studentVO);
}

List互转


@Mapper
public interface StudentMapper {

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

    @Mappings({
            @Mapping(source = "name", target = "studentName"),
            @Mapping(source = "age", target = "studentAge")
    })
    StudentVO po2Vo(StudentPO studentPO);

    List<StudentVO> poList2VoList(List<StudentPO> studentPO);
}

List类型互转的映射规则会用单个对象的映射规则,看测试效果


@Test
public void poList2VoList() {
    List<StudentPO> studentPOList = new ArrayList<>();
    for (int i = 1; i <= 2; i++) {
        StudentPO studentPO = StudentPO.builder().id(i).name(String.valueOf(i)).age(i).build();
        studentPOList.add(studentPO);
    }
    List<StudentVO> studentVOList = StudentMapper.INSTANCE.poList2VoList(studentPOList);
    // [StudentVO(id=1, studentName=1, studentAge=1, schoolName=null),
    // StudentVO(id=2, studentName=2, studentAge=2, schoolName=null)]
    System.out.println(studentVOList);
}

多个对象映射一个对象

我们用SchoolPO和StudentPO来映射SchoolStudentVO


@Data
@Builder
public class SchoolPO {

    private String name;
    private String location;
}

@Data
@Builder
public class StudentPO {

    private Integer id;
    private String name;
    private Integer age;
    private String className;
}

@Data
public class SchoolStudentVO {

    private String schoolName;
    private String studentName;
}

@Mapper
public interface StudentMapper {

    @Mappings({
            @Mapping(source = "schoolPO.name", target = "schoolName"),
            @Mapping(source = "studentPO.name", target = "studentName")
    })
    SchoolStudentVO mergeVo(SchoolPO schoolPO, StudentPO studentPO);
}

测试例子如下


@Test
public void mergeVo() {
    SchoolPO schoolPO = SchoolPO.builder().name("学校名字").build();
    StudentPO studentPO = StudentPO.builder().name("学生名字").build();
    SchoolStudentVO schoolStudentVO = StudentMapper.INSTANCE.mergeVo(schoolPO, studentPO);
    // SchoolStudentVO(schoolName=学校名字, studentName=学生名字)
    System.out.println(schoolStudentVO);
}

当然还有其他的骚操作,这里就简单介绍一些比较实用的技巧,有兴趣的可以看官方的example

https://github.com/mapstruct/mapstruct-examples

实现原理

MapStruct帮你对接口生成了一个实现类,下面就是生成的实现类,从class文件夹中可以看到


public class StudentMapperImpl implements StudentMapper {
    public StudentMapperImpl() {
    }

    public StudentVO po2Vo(StudentPO studentPO) {
        if (studentPO == null) {
            return null;
        } else {
            StudentVO studentVO = new StudentVO();
            studentVO.setStudentAge(studentPO.getAge());
            studentVO.setStudentName(studentPO.getName());
            studentVO.setId(studentPO.getId());
            return studentVO;
        }
    }

    public List<StudentVO> poList2VoList(List<StudentPO> studentPO) {
        if (studentPO == null) {
            return null;
        } else {
            List<StudentVO> list = new ArrayList(studentPO.size());
            Iterator var3 = studentPO.iterator();

            while(var3.hasNext()) {
                StudentPO studentPO1 = (StudentPO)var3.next();
                list.add(this.po2Vo(studentPO1));
            }

            return list;
        }
    }

    public SchoolStudentVO mergeVo(SchoolPO schoolPO, StudentPO studentPO) {
        if (schoolPO == null && studentPO == null) {
            return null;
        } else {
            SchoolStudentVO schoolStudentVO = new SchoolStudentVO();
            if (schoolPO != null) {
                schoolStudentVO.setSchoolName(schoolPO.getName());
            }

            if (studentPO != null) {
                schoolStudentVO.setStudentName(studentPO.getName());
            }

            return schoolStudentVO;
        }
    }
}

最后,再附上我历时三个月总结的 Java 面试 + Java 后端技术学习指南,笔者这几年及春招的总结,github 1.4k star,拿去不谢!

下载方式1. 首先扫描下方二维码
2. 后台回复「Java面试」即可获取

原文地址:https://sihai.blog.csdn.net/article/details/109466010

本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

转载声明:转载请注明出处,本技术博客是本人原创文章

本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

原文链接:blog.ouyangsihai.cn >> 遇到MapStruct后,再也不手写PO,DTO,VO对象之间的转换了


 上一篇
挺带劲,这款国人开源的监控系统功能真强大~ 挺带劲,这款国人开源的监控系统功能真强大~
点击上方 **好好学java **,选择 **星标 **公众号 重磅资讯、干货,第一时间送达 今日推荐:个人原创100W+访问量博客:点击前往,查看更多 来源:小黄鸭编程社区 项目简介集监控点监控、日志监控、数据可视化以及监控告警为
2021-04-04
下一篇 
NIO这样理解才够深入! NIO这样理解才够深入!
点击上方 **好好学java **,选择 **星标 **公众号 重磅资讯、干货,第一时间送达 今日推荐:个人原创100W+访问量博客:点击前往,查看更多 作者:Ruheng 链接:https://www.jianshu.com/p
2021-04-04