MapStruct 映射列表

使用 Mapstruct,我们可以以与映射 primitives 类似的方式映射列表。 要获取对象列表,我们应该提供一个可以映射对象的映射器方法。

语法

@Mapper
public interface CarMapper {
List<String> getListOfStrings(List<Integer> listOfIntegers);
List<Car> getCars(List<CarEntity> carEntities);
Car getModelFromEntity(CarEntity carEntity);
}

示例

打开使用 defaultExpression 映射章节中更新的项目映射。

使用以下代码更新 CarEntity.java

CarEntity.java ``java ackage com.jiyik.entity;

mport java.util.GregorianCalendar;

ublic class CarEntity { private int id; private double price; private GregorianCalendar manufacturingDate; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public GregorianCalendar getManufacturingDate() { return manufacturingDate; } public void setManufacturingDate(GregorianCalendar manufacturingDate) { this.manufacturingDate = manufacturingDate; } public String getName() { return name; } public void setName(String name) { this.name = name; }

``

使用以下代码更新 Car.java

Car.java

package com.jiyik.model;

public class Car {
    private int id;
    private String price;
    private String manufacturingDate;
    private String brand;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    public String getManufacturingDate() {
        return manufacturingDate;
    }
    public void setManufacturingDate(String manufacturingDate) {
        this.manufacturingDate = manufacturingDate;
    }
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

使用以下代码更新 CarMapper.java

CarMapper.java

package com.jiyik.mapper;

import com.jiyik.entity.CarEntity;
import com.jiyik.model.Car;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.util.List;
import java.util.UUID;

@Mapper( imports = UUID.class )
public interface CarMapper {
    @Mapping(source = "name", target = "name", defaultExpression = "java(UUID.randomUUID().toString())")
    @Mapping(target = "brand", constant = "BMW")
    @Mapping(source = "price", target = "price", numberFormat = "$#.00")
    @Mapping(source = "manufacturingDate", target = "manufacturingDate", dateFormat = "dd.MM.yyyy")
    Car getModelFromEntity(CarEntity carEntity);

    List<String> getListOfStrings(List<Integer> listOfIntegers);
    List<Car> getCars(List<CarEntity> carEntities);
}

使用以下代码更新 CarMapperTest.java

CarMapperTest.java

import com.jiyik.entity.CarEntity;
import com.jiyik.mapper.CarMapper;
import com.jiyik.model.Car;
import org.junit.Test;
import org.mapstruct.factory.Mappers;

import java.util.Arrays;
import java.util.GregorianCalendar;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class CarMapperTest {
    private CarMapper carMapper = Mappers.getMapper(CarMapper.class);

    @Test
    public void testEntityToModel() {
        CarEntity entity = new CarEntity();
        entity.setPrice(345000);
        entity.setId(1);
        entity.setManufacturingDate(new GregorianCalendar(2015, 3, 5));

        CarEntity entity1 = new CarEntity();
        entity1.setPrice(445000);
        entity1.setId(2);
        entity1.setManufacturingDate(new GregorianCalendar(2015, 3, 5));

        List<CarEntity> carEntities = Arrays.asList(entity, entity1);

        Car model = carMapper.getModelFromEntity(entity);
        assertEquals("$345000.00",model.getPrice());
        assertEquals(entity.getId(), model.getId());

        assertEquals("BMW", model.getBrand());
        assertEquals("05.04.2015", model.getManufacturingDate());

        List<Integer> list = Arrays.asList(1,2,3);
        List<String> listOfStrings = carMapper.getListOfStrings(list);
        List<Car> listOfCars = carMapper.getCars(carEntities);

        assertEquals(3, listOfStrings.size());
        assertEquals(2, listOfCars.size());
    }
}

运行以下命令来测试映射。

$ mvn clean test

执行结果如下

MapStruct 映射列表

查看笔记

扫码一下
查看教程更方便