乐趣区

关于javascript:MapStruct入门使用

文章目录

在理论的我的项目开发中会大量的应用 POJO 作为数据载体,通常有如下的:

实体名 实体含意 阐明
DO Data Object 与数据库表构造一一对应,通过 DAO 层向上传输数据源对象
DTO Data Transfer Object 数据传输对象,Service 或 Manager 向外传输的对象
BO Business Object 业务对象,由 Service 层输入的封装业务逻辑的对象
VO View Object 显示层对象,通常是 Web 向模板渲染引擎层传输的对象
  • Spring BeanUtils
  • Apache BeanUtils
  • Orika 的 MapperFacade 和 MapperFactory
  • MapStruct
    能够抉择 MapStruct,性能非常弱小,而且很灵便,能够满足咱们须要的各种需要。

入门案例

Maven 我的项目引入

只须要在 pom.xml 文件中退出以下依赖即可:

...
<properties>
        <java.version>1.8</java.version>
        <mapstruct.version>1.3.1.Final</mapstruct.version>
    </properties>
...
    <dependencies>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>
    </dependencies>
...
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${mapstruct.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>
...
package com.linfanchen.springboot.lab.mapstruct.domain;

public class CarDO { 
    /**
     * 主键 ID
     */
    private Integer id;

    /**
     * 名称
     */
    private String name;

    /**
     * 品牌
     */
    private String brand;

    /**
     * 车价
     */
    private Integer price;

    /**
     * 轮胎属性
     */
    private TyreDO tyreDO;

    /**
     * 汽车图片
     */
    private String images;

    public  String  getImages()  {return images;}

    public  void  setImages(String images)  {this.images = images;}

    public  TyreDO  getTyreDO()  {return tyreDO;}

    public  void  setTyreDO(TyreDO tyreDO)  {this.tyreDO = tyreDO;}

    public  Integer  getId()  {return id;}

    public  void  setId(Integer id)  {this.id = id;}

    public  String  getName()  {return name;}

    public  void  setName(String name)  {this.name = name;}

    public  String  getBrand()  {return brand;}

    public  void  setBrand(String brand)  {this.brand = brand;}

    public  Integer  getPrice()  {return price;}

    public  void  setPrice(Integer price)  {this.price = price;}
}</pre>

<pre class="prettyprint hljs cs">package com.linfanchen.springboot.lab.mapstruct.domain;

/**
 * 轮胎
 */
public class TyreDO { 
    /**
     * 主键 ID
     */
    private Integer id;

    /**
     * 轮胎名称
     */
    private String name;

    /**
     * 轮胎尺寸
     */
    private Integer size;

    public  Integer  getId()  {return id;}

    public  void  setId(Integer id)  {this.id = id;}

    public  String  getName()  {return name;}

    public  void  setName(String name)  {this.name = name;}

    public  Integer  getSize()  {return size;}

    public  void  setSize(Integer size)  {this.size = size;}
}

指标对象

package com.linfanchen.springboot.lab.mapstruct.bo;

import com.linfanchen.springboot.lab.mapstruct.domain.TyreDO;

import java.util.List;

/**
 * 汽车业务对象
 */
public class CarBO { 
    /**
     * 主键 ID
     */
    private Integer id;

    /**
     * 名称
     */
    private String name;

    /**
     * 品牌
     */
    private String brand;

    /**
     * 车价
     */
    private Integer price;

    /**
     * 轮胎属性
     */
    private TyreDO tyreDO;

    /**
     * 汽车图片
     */
    private String images;

    public  Integer  getId()  {return id;}

    public  void  setId(Integer id)  {this.id = id;}

    public  String  getName()  {return name;}

    public  void  setName(String name)  {this.name = name;}

    public  String  getBrand()  {return brand;}

    public  void  setBrand(String brand)  {this.brand = brand;}

    public  Integer  getPrice()  {return price;}

    public  void  setPrice(Integer price)  {this.price = price;}

    public  TyreDO  getTyreDO()  {return tyreDO;}

    public  void  setTyreDO(TyreDO tyreDO)  {this.tyreDO = tyreDO;}

    public  String  getImages()  {return images;}

    public  void  setImages(String images)  {this.images = images;}
}

CarDetailBO

package com.linfanchen.springboot.lab.mapstruct.bo;

import com.linfanchen.springboot.lab.mapstruct.domain.TyreDO;

import java.util.List;

/**
 * 汽车业务对象
 */
public class CarDetailBO { 
    /**
     * 主键 ID
     */
    private Integer carId;

    /**
     * 名称
     */
    private String carName;

    /**
     * 品牌
     */
    private String carBrand;

    /**
     * 车价
     */
    private Integer price;

    /**
     * 轮胎属性
     */
    private TyreDO tyreDetail;

    /**
     * 汽车图片
     */
    private List<String> images;

    public  Integer  getCarId()  {return carId;}

    public  void  setCarId(Integer carId)  {this.carId = carId;}

    public  String  getCarName()  {return carName;}

    public  void  setCarName(String carName)  {this.carName = carName;}

    public  String  getCarBrand()  {return carBrand;}

    public  void  setCarBrand(String carBrand)  {this.carBrand = carBrand;}

    public  Integer  getPrice()  {return price;}

    public  void  setPrice(Integer price)  {this.price = price;}

    public  TyreDO  getTyreDetail()  {return tyreDetail;}

    public  void  setTyreDetail(TyreDO tyreDetail)  {this.tyreDetail = tyreDetail;}

    public List<String> getImages() {return images;}

    public  void  setImages(List<String> images)  {this.images = images;}
}

转换器 CarConvert

阐明:

  • INSTANCE 是 MapStruct 为咱们提供的映射工厂,指定接口类型后主动帮咱们创立接口的实现,且保障是线程平安的单例,无需本人手动创立。

    package com.linfanchen.springboot.lab.mapstruct.convert;
    
    import com.linfanchen.springboot.lab.mapstruct.bo.CarBO;
    import com.linfanchen.springboot.lab.mapstruct.bo.CarDetailBO;
    import com.linfanchen.springboot.lab.mapstruct.domain.CarDO;
    import org.apache.commons.lang3.StringUtils;
    import org.mapstruct.Mapper;
    import org.mapstruct.Mapping;
    import org.mapstruct.Mappings;
    import org.mapstruct.Named;
    import org.mapstruct.factory.Mappers;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    /**
     * 转换类
     */
    @Mapper
    public interface  CarConvert  {CarConvert INSTANCE = Mappers.getMapper(CarConvert.class);
    
    /**
     * 起源和指标类的间接转换
     */
    CarBO convert(CarDO carDO);
    
    /**
     * 增加字段解决和映射关系后的转换
     */
    @Mappings({@Mapping(source = "id", target = "carId"),
            @Mapping(source = "name", target = "carName"),
            @Mapping(source = "brand", target = "carBrand"),
            @Mapping(source = "tyreDO.id", target = "tyreDetail.id"),
            @Mapping(source = "tyreDO.name", target = "tyreDetail.name"),
            @Mapping(source = "tyreDO.size", target = "tyreDetail.size"),
            @Mapping(source = "images", target = "images", qualifiedByName = "convertImagesStrToList")
    
    })
    CarDetailBO convertDetail(CarDO carDO);
    
    @Named("convertImagesStrToList")
    default List<String> convertImagesStrToList(String images) {String[] splitArr = org.springframework.util.StringUtils.split(images, ",");
        return new ArrayList<>(Arrays.asList(splitArr));
    }
    
    }

    应用 CarTest

    package com.linfanchen.springboot.lab.mapstruct;
    
    import com.linfanchen.springboot.lab.mapstruct.bo.CarBO;
    import com.linfanchen.springboot.lab.mapstruct.bo.CarDetailBO;
    import com.linfanchen.springboot.lab.mapstruct.convert.CarConvert;
    import com.linfanchen.springboot.lab.mapstruct.domain.CarDO;
    import com.linfanchen.springboot.lab.mapstruct.domain.TyreDO;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class  CarTest  { 
    
    @Test
    public void test() { 
        // 创立轮胎对象
        TyreDO tyreDO = new TyreDO();
        tyreDO.setId(888);
        tyreDO.setName("米其林 245/45R19");
        tyreDO.setSize(19);
    
        // 汽车图片地址,通过半角逗号分隔的 http 地址
        List<String> imageList = new ArrayList<>();
        imageList.add("https://www.bmw.com.cn/image.1601043751651.jpg");
        imageList.add("https://www.bmw.com.cn/image.1623295725814.jpg");
        imageList.add("https://www.bmw.com.cn/image.1611042489480.jpg");
        String imageStr = imageList.stream().map(String::valueOf).collect(Collectors.joining(","));
    
        // 创立汽车对象
        CarDO carDO = new CarDO();
        carDO.setId(123);
        carDO.setBrand("BMW");
        carDO.setName("宝马 530Li 行政版");
        carDO.setPrice(540000);
        carDO.setTyreDO(tyreDO);
        carDO.setImages(imageStr);
    
        // 间接的数据拷贝
        CarBO carBO = CarConvert.INSTANCE.convert(carDO);
        System.out.println(carBO.getImages());
        System.out.println(carBO.getBrand());
        System.out.println(carBO.getName());
        System.out.println(carBO.getTyreDO());
    
        // 带数据转换的拷贝
        CarDetailBO carDetailBO = CarConvert.INSTANCE.convertDetail(carDO);
        System.out.println(carDetailBO.getImages());
        System.out.println(carDetailBO.getTyreDetail().getName());
    }
    
    }

    在须要进行对象的转换的时候,咱们能够应用 MapStruct 进行各种灵便的转换,最终失去咱们想要的数据。

退出移动版