Spring Boot集成mapstruct快速入门指南

1.mapstruct介绍

在项目中经常会用到DO,BO,DTO,VO等对象的相互转化,这就需要一个高效通用的转化工具,毕竟每个字段get/set方法会很麻烦。MapStruct 就是这样的一个属性映射工具,只需要定义一个 Mapper 接口,MapStruct就会自动实现这个映射接口,避免了复杂繁琐的映射实现。

2.代码工程

pom.xml




    
        springboot-demo
        com.et
        1.0-SNAPSHOT
    
    4.0.0


    mapstruct


    
        8
        8
    
    


        
            org.springframework.boot
            spring-boot-starter-web
        


        
            org.springframework.boot
            spring-boot-autoconfigure
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.mapstruct
            mapstruct
            1.3.1.Final
        
        
            org.mapstruct
            mapstruct-processor
            1.3.1.Final
        
        
            org.projectlombok
            lombok
            provided
        
        
            com.alibaba
            fastjson
            2.0.40
            compile
        
    

配置文件


server:
  port: 8088

启动类


package com.et.mapstruct;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class DemoApplication {


   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
}

entity


package com.et.mapstruct.entity;


import lombok.Data;


@Data
public class Car {
    private String make;
    private int numberOfSeats;
    private CarType carType;


}

package com.et.mapstruct.entity;


import lombok.Data;




@Data
public class CarDTO {
    private String make;
    private int seatCount;
    private String type;
}

package com.et.mapstruct.entity;


import lombok.Data;




@Data
public class CarType {
    private String type;
}

mapper


package com.et.mapstruct.mapper;


import com.et.mapstruct.entity.Car;
import com.et.mapstruct.entity.CarDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;


import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;


@Mapper(componentModel = "spring",imports = {LocalDateTime.class, Date.class, ZoneId.class})//交给spring管理
public interface CarMapping {
    
    /**
     * 用来调用实例 实际开发中可使用注入Spring  不写
     */
    CarMapping CAR_MAPPING = Mappers.getMapper(CarMapping.class);


    /**
     *  源类型 目标类型 成员变量相同类型 相同变量名 不用写{@link org.mapstruct.Mapping}来映射
     *
     * @param car the car
     * @return the car dto
     */
    @Mapping(target = "type", source = "carType.type")
    @Mapping(target = "seatCount", source = "numberOfSeats")
    CarDTO carToCarDTO(Car car);
}

上面短短几行代码就可以了十分简单!解释一下操作步骤: 首先声明一个映射接口用@org.mapstruct.Mapper (不要跟mybatis注解混淆)标记,说明这是一个实体类型转换接口。这里我们声明了一个 CAR_MAPPING 来方便我们调用,CarDTO toCarDTO(Car car)是不是很熟悉, 像mybatis一样抽象出我们的转换方法。@org.mapstruct.Mapping注解用来声明成员属性的映射。该注解有两个重要的属性:

  • source 代表转换的源。这里就是Car 。

  • target 代表转换的目标。这里是CarDTO 。

这里以成员变量的参数名为依据,如果有嵌套比如 Car 里面有个 CarType 类型的成员变量 carType,其 type 属性 来映射 CarDTO 中的 type 字符串,我们使用 type.type 来获取属性值。如果有多层以此类推。MapStruct 最终调用的是 setter 和 getter 方法,而非反射。这也是其性能比较好的原因之一。numberOfSeats 映射到 seatCount 就比较好理解了。我们是不是忘记了一个属性 make,因为他们的位置且名称完全一致,所以可以省略。而且对于包装类是自动拆箱封箱操作的,并且是线程安全的。MapStruct不单单有这些功能,还有其他一些复杂的功能

代码仓库

  • https://github.com/Harries/springboot-demo

3.测试


package com.et.mapstruct;


import com.alibaba.fastjson2.JSONObject;
import com.et.mapstruct.entity.Car;
import com.et.mapstruct.entity.CarDTO;
import com.et.mapstruct.entity.CarType;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;




@RunWith(SpringRunner.class)
@SpringBootTest
public class CarMappingTest {


    private Car car;
    private CarDTO carDTO;


    @Autowired
    private com.et.mapstruct.mapper.CarMapping CarMapping;


    @Before
    public void setUp() throws Exception {
        car = new Car();
        car.setMake("make");
        CarType type =  new CarType();
        type.setType("type");
        car.setCarType(type);
        car.setNumberOfSeats(1);
    }


    @Test
    public void testcarToCarDTO() {
        carDTO = CarMapping.carToCarDTO(car);
        System.out.println(JSONObject.toJSONString(carDTO));
    }




}

输出结果


{"make":"make","seatCount":1,"type":"type"}

4.引用

  • https://mapstruct.org/documentation/stable/reference/html/#defining-mapper

  • http://www.liuhaihua.cn/archives/710319.html

你可能感兴趣的:(spring,boot,后端,java,spring)