使用MapStruct 进行对象属性转换

在一个成熟的工程中,尤其是现在的分布式系统中,应用与应用之间,还有单独的应用细分模块之后,DO 一般不会让外部依赖,这时候需要在提供对外接口的模块里放 DTO 用于对象传输,也即是 DO 对象对内,DTO对象对外,DTO 可以根据业务需要变更,并不需要映射 DO 的全部属性;这种对象与对象之间的互相转换,就需要有一个专门用来解决转换问题的工具,毕竟每一个字段都 get/set 会很麻烦。

官网地址:https://mapstruct.org/

第一步 :引入依赖

<properties>
    <mapstruct.version>1.3.1.Final</mapstruct.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>${mapstruct.version}</version>
    </dependency>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-jdk8</artifactId>
        <version>${mapstruct.version}</version>
    </dependency>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>${mapstruct.version}</version>
    </dependency>
</dependencies>

第二步:创建mapper接口

加入

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

如果属性名都可以对上的话 那么就没必要使用@mapping  如果要将某个值赋给某个的话  那么就需要使用。 

下边的实体类只是演示

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

    @Mapping(source = "gender.name", target = "gender")
    @Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss")
    StudentVO student2StudentVO(Student student);

    List<StudentVO> students2StudentVOs(List<Student> studentList);

    @Mapping(source = "student.gender.name", target = "gender")
    @Mapping(source = "student.birthday", target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(source = "course.id", target = "id")
    @Mapping(source = "course.courseName", target = "course")
    @Mapping(source = "aaalist", target = "studentList")
    StudentVO studentAndCourse2StudentVO(Student student, Course course, List<Student> aaalist);

    @Data
    public class Student {
        private long id;
        private String name;
        private int age;
        private GenderEnum gender;
        private Double height;
        private Date birthday;
    }

    @Data
    public class Course {

        private String courseName;
        private int sortNo;
        private long id;
    }

    @Data
    public class StudentVO {

        private long id;
        private String name;
        private int age;
        private String gender;
        private Double height;
        private String birthday;
        private String course;

        private List<StudentVO> studentList;
    }

    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;
        }
    }
}

如何使用--------------------------------------------------------------------------------------------------------------------

public class DemoTest {
    public /*static*/ void main(String[] args) {
        DemoMapper.Student student = new DemoMapper.Student();
        student.setId(11111);
        student.setName("张三");
        student.setAge(23);
        student.setBirthday(new Date());
        student.setGender(DemoMapper.GenderEnum.Male);
        student.setHeight(160.5);

        DemoMapper.Course course = new DemoMapper.Course();
        course.setId(22222);
        course.setCourseName("语文");
        course.setSortNo(3);
// 下边一句就是在进行转换 
        DemoMapper.StudentVO studentVO1 = DemoMapper.INSTANCE.student2StudentVO(student);
        System.out.println(studentVO1);

        List<DemoMapper.Student> list = new ArrayList<>();
        list.add(student);
// 下边一句就是在进行转换
        List<DemoMapper.StudentVO> result = DemoMapper.INSTANCE.students2StudentVOs(list);
        System.out.println(result);
// 下边一句就是在进行转换
        DemoMapper.StudentVO studentVO2 = DemoMapper.INSTANCE.studentAndCourse2StudentVO(student, course, list);
        System.out.println(studentVO2);
    }
}

工作笔记   仅供参考 。。。。。。。。。。 如有问题请进行留言。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值