mapstruct实体类转换工具

1、mapstruct环境搭建

1、导入依赖文件

	<properties>
        <org.mapstruct.version>1.4.2.Finalorg.mapstruct.version>
    properties>



     <dependency>
         <groupId>org.mapstructgroupId>
         <artifactId>mapstructartifactId>
         <version>${org.mapstruct.version}version>
     dependency>
     <dependency>
         <groupId>org.mapstructgroupId>
         <artifactId>mapstruct-processorartifactId>
         <scope>providedscope>
         <version>1.2.0.Finalversion>
     dependency>


	<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.8.1version>
                <configuration>
                    <source>1.8source>
                    
                    <target>1.8target>
                    
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstructgroupId>
                            <artifactId>mapstruct-processorartifactId>
                            <version>${org.mapstruct.version}version>
                        path>
                        
                    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);
    }
}

测试结果:
mapstruct实体类转换工具_第1张图片

你可能感兴趣的:(java工具类,java,maven,intellij-idea)