MapStruct 使用 expression

MapStruct 允许调用自定义逻辑的转换方法。 我们可以使用表达式来实现相同的目的,我们可以传递任何 java 对象并调用其方法进行转换。

语法

@Mapping(target = "target-property",
expression = "java(target-method())")
  • target-property - 我们正在为其进行映射的属性。
  • expression - 映射器将调用表达式中编写的 java 方法。
  • target-method - 目标方法是要调用的方法。 如果方法存在于不同的类中,请使用 new class-name.target-method()

示例

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

使用以下代码更新 CarEntity.java

CarEntity.java

package com.jiyik.entity;

import java.util.GregorianCalendar;

public class CarEntity {
    private int id;
    private double price;
    private GregorianCalendar manufacturingDate;
    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;
    }
}

使用以下代码更新 Car.java

Car.java

package com.jiyik.model;

public class Car {
    private int id;
    private String price;
    private String manufacturingDate;
    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;
    }
}

使用以下代码更新 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.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

@Mapper
public interface CarMapper {
    @Mapping(source = "price", target = "price", numberFormat = "$#.00")
    @Mapping(target = "manufacturingDate",
            expression = "java(getManufacturingDate(carEntity.getManufacturingDate()))")
    Car getModelFromEntity(CarEntity carEntity);

    default String getManufacturingDate(GregorianCalendar manufacturingDate) {
        Date d = manufacturingDate.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat( "dd.MM.yyyy" );
        return sdf.format( d );
    }
}

使用以下代码更新 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.GregorianCalendar;

import static org.junit.Assert.assertEquals;

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));
        Car model = carMapper.getModelFromEntity(entity);
        assertEquals(model.getPrice(), "$345000.00");
        assertEquals(entity.getId(), model.getId());
        assertEquals("05.04.2015", model.getManufacturingDate());
    }
}

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

$ mvn clean test

执行结果如下

MapStruct 使用 expression

查看笔记

扫码一下
查看教程更方便