mapstruct实体类转换工具

文章介绍了如何搭建MapStruct环境,包括添加依赖和相关插件配置。接着展示了Car和CarDto两个类的定义,以及使用MapStruct的Mapper接口进行对象转换的方法。最后提供了一个测试类MsTest来验证转换功能。
摘要由CSDN通过智能技术生成

1、mapstruct环境搭建

1、导入依赖文件

	<properties>
        <org.mapstruct.version>1.4.2.Final</org.mapstruct.version>
    </properties>


<!--mapStruct依赖-->
     <dependency>
         <groupId>org.mapstruct</groupId>
         <artifactId>mapstruct</artifactId>
         <version>${org.mapstruct.version}</version>
     </dependency>
     <dependency>
         <groupId>org.mapstruct</groupId>
         <artifactId>mapstruct-processor</artifactId>
         <scope>provided</scope>
         <version>1.2.0.Final</version>
     </dependency>

<!--mapStruct相关插件-->
	<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <!-- depending on your project -->
                    <target>1.8</target>
                    <!-- depending on your project -->
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                        <!-- other annotation processors -->
                    </annotationProcessorPaths>
                </configuration></plugin></plugins>
    </build>

2、创建car类

package cn.itedus.lottery.test.mapstruct;

import cn.itedus.lottery.test.mapstruct.entity.Brand;

/**
 * @author cl
 */
public class Car {

    private String make;

    private int numberOfSeats;

    private Brand brand;

    public Car(String make, int numberOfSeats, Brand brand) {
        this.make = make;
        this.numberOfSeats = numberOfSeats;
        this.brand = brand;
    }

    public Car() {
    }

    public Car(String make, int numberOfSeats) {
        this.make = make;
        this.numberOfSeats = numberOfSeats;
    }

    public String getMake() {
        return make;
    }

    public int getNumberOfSeats() {
        return numberOfSeats;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public void setNumberOfSeats(int numberOfSeats) {
        this.numberOfSeats = numberOfSeats;
    }

    public Brand getBrand() {
        return brand;
    }

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

    @Override
    public String toString() {
        return "Car{" +
                "make='" + make + '\'' +
                ", numberOfSeats=" + numberOfSeats +
                ", brand=" + brand +
                '}';
    }
}


3、创建carDto类

package cn.itedus.lottery.test.mapstruct;

import cn.itedus.lottery.test.mapstruct.entity.Brand;

/**
 * @author cl
 */
public class CarDto {

    private String make;

    private int seatCount;

    private String type;

    private Brand carBrand;



    public CarDto() {
    }

    public CarDto(String make, int seatCount, String type) {
        this.make = make;
        this.seatCount = seatCount;
        this.type = type;
    }


    public String getMake() {
        return make;
    }

    public int getSeatCount() {
        return seatCount;
    }

    public String getType() {
        return type;
    }

    public void setMake(String make) {
        this.make = make;
    }

    public void setSeatCount(int seatCount) {
        this.seatCount = seatCount;
    }

    public void setType(String type) {
        this.type = type;
    }

    public CarDto(String make, int seatCount, String type, Brand carBrand) {
        this.make = make;
        this.seatCount = seatCount;
        this.type = type;
        this.carBrand = carBrand;
    }

    public Brand getCarBrand() {
        return carBrand;
    }

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

    @Override
    public String toString() {
        return "CarDto{" +
                "make='" + make + '\'' +
                ", seatCount=" + seatCount +
                ", type='" + type + '\'' +
                ", carBrand=" + carBrand +
                '}';
    }
}


4、创建CarMapper类

package cn.itedus.lottery.test.mapstruct;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

/**
 * @author cl
 */
@Mapper
public interface CarMapper {

    CarMapper instantce = Mappers.getMapper(CarMapper.class);

    /**
     * Car -> CarDto转换
     *
     * 将源类Car中的字段numberOfSeats转换到CarDto中的seatCount字段中
     *
     * 表达式如果想自动提示的话,则需要安装mapstruct support插件
     *
     * @param car
     * @return
     */
    @Mapping(source = "numberOfSeats", target = "seatCount")
    @Mapping(target = "type",constant = "bbb")
    CarDto carToCarDto(Car car);


    @Mappings({@Mapping(source = "numberOfSeats", target = "seatCount"),
            @Mapping(target = "type",constant = "bbb"),
            @Mapping(source = "brand",target = "carBrand")
    })
    CarDto carToCarDtoV2(Car car);

}


5、测试类

public class MsTest {

    @Test
    public void test_ms(){
        CarMapper instantce = CarMapper.instantce;

        Car car = new Car();
        car.setMake("from");
        car.setNumberOfSeats(100);
        car.setBrand(new Brand("宝马"));
        CarDto carDto = instantce.carToCarDtoV2(car);
        System.out.println(car);
        System.out.println(carDto);
    }
}

测试结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值