MapStruct转换类工具使用

为什么使用MapStruct?

1.效率高:非反射,比BeanUtils效率高10-20倍;

2.直观:把类转换标准以配置的形式展示;

3.灵活:支持多种转换形式;类合并:实体A + 实体B → 实体C 。数组类转换: Lists.of(实体A1,实体A2,实体A3) → List<实体A(VO)>

              并且对属性也可以增加条件过滤

4.深拷贝:不会对老对象有影响

如果说缺点的话,就是编码稍微复杂一些。。

测试类一共涉及到以下这些:

 

 

 

<!--  mapStruct开始  -->
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>1.4.2.Final</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-jdk8</artifactId>
            <version>1.4.2.Final</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>1.4.2.Final</version>
        </dependency>
        <!--  mapStruct结束  -->

 

package com.charkey.digest.mapstruct;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Student implements Serializable {

    private Integer id;

    private String name;

    private Integer age;

    private String local;
}
package com.charkey.digest.mapstruct;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class StudentVO implements Serializable {

    private Integer id;

    private String idCard;

    private String studentName;

    private Integer studentAge;

}
package com.charkey.digest.mapstruct;

import org.mapstruct.InjectionStrategy;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

import java.util.List;

@Mapper(componentModel = "spring",injectionStrategy = InjectionStrategy.CONSTRUCTOR)
public interface StudentMapStruct {

    @Mappings({
            @Mapping(target = "name",source = "studentName"),
            @Mapping(target = "age", source = "studentAge"),
            @Mapping(target = "local", expression = "java(com.charkey.digest.mapstruct.MapStructUtils.localConvert(vo.getIdCard()))")
    })
    Student studentConvert(StudentVO vo);

    List<Student> studentListConvert(List<StudentVO> vos);

    @Mappings({
            @Mapping(target = "studentName",source = "vo1.studentName"),
            @Mapping(target = "teacherName", source = "vo2.teacherName"),
    })
    StudentAndTeacher stuAndTchrConvert(StudentVO vo1,TeacherVO vo2);
}
package com.charkey.digest.mapstruct;

public class MapStructUtils {

    public static String localConvert(String idCard){
        return idCard.startsWith("110")?"京户":"非京户";
    }
}
package com.charkey.digest.mapstruct;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class TeacherVO implements Serializable {

    private Integer id;

    private String teacherName;

    private Integer teacherAge;

}
package com.charkey.digest.mapstruct;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class StudentAndTeacher implements Serializable {

    private Integer id;

    private String studentName;

    private String teacherName;
}
package com.charkey.digest.mapstruct;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("mapstruct")
@Slf4j
public class TestController {

    @Autowired
    private StudentMapStruct studentMapStruct;

    @GetMapping("convert")
    public String convert() {
        StudentVO vo = new StudentVO(1,"110109200304091122","学生A",15);
        Student student = studentMapStruct.studentConvert(vo);
        log.info("转换前:{},转换后:{}",vo.toString(),student.toString());

        return String.format("转换前:%s,转换后:%s",vo.toString(),student.toString());
    }

    @GetMapping("convert2")
    public String convert2() {
        StudentVO vo = new StudentVO(1,"110109200304091122","学生A",15);
        StudentVO vo1 = new StudentVO(2, "130109200311051122", "学生B", 18);
        List<StudentVO> studentVOS = new ArrayList<>();
        studentVOS.add(vo);
        studentVOS.add(vo1);
        List<Student> students = studentMapStruct.studentListConvert(studentVOS);
        return String.format("转换前:%s,转换后:%s",studentVOS.toString(),students.toString());
    }

    @GetMapping("convert3")
    public String convert3() {
        StudentVO vo = new StudentVO(1,"110109200304091122","学生A",15);
        TeacherVO vo1 = new TeacherVO(2, "老师A", 18);

        StudentAndTeacher studentAndTeacher = studentMapStruct.stuAndTchrConvert(vo, vo1);
        return String.format("转换:%s",studentAndTeacher.toString());
    }
}

 

posted @ 2023-03-08 11:26  蔡徐坤1987  阅读(700)  评论(0编辑  收藏  举报