English 中文(简体)
MapStruct - Using expression
  • 时间:2024-12-22

MapStruct - Using expression


Previous Page Next Page  

MapStruct allows to call a conversion method for customized logic. We can use expression to achieve the same where we can pass any java object and call its method to do the conversion.

Syntax


@Mapping(target = "target-property", 
   expression = "java(target-method())")

Here

    target-property − the property for which we are doing the mapping.

    expression − mapper will call the java method written in the expression.

    target-method − target-method is the method to be called. In case method is present in different class, use new class-name.target-method()

Example

Open project mapping as updated in Mapping Using dateFormat chapter in Ecppse.

Update CarEntity.java with following code −

CarEntity.java


package com.tutorialspoint.entity;
import java.util.GregorianCalendar;

pubpc class CarEntity {
   private int id;
   private double price;
   private GregorianCalendar manufacturingDate;
   pubpc int getId() {
      return id;
   }
   pubpc void setId(int id) {
      this.id = id;
   }
   pubpc double getPrice() {
      return price;
   }
   pubpc void setPrice(double price) {
      this.price = price;
   }
   pubpc GregorianCalendar getManufacturingDate() {
      return manufacturingDate;
   }
   pubpc void setManufacturingDate(GregorianCalendar manufacturingDate) {
      this.manufacturingDate = manufacturingDate;
   }
}

Update Car.java with following code −

Car.java


package com.tutorialspoint.model;

pubpc class Car {
   private int id;
   private String price;
   private String manufacturingDate;
   pubpc int getId() {
      return id;
   }
   pubpc void setId(int id) {
      this.id = id;
   }
   pubpc String getPrice() {
      return price;
   }
   pubpc void setPrice(String price) {
      this.price = price;
   }
   pubpc String getManufacturingDate() {
      return manufacturingDate;
   }
   pubpc void setManufacturingDate(String manufacturingDate) {
      this.manufacturingDate = manufacturingDate;
   }
}

Update CarMapper.java with following code −

CarMapper.java


package com.tutorialspoint.mapper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import com.tutorialspoint.entity.CarEntity;
import com.tutorialspoint.model.Car;

@Mapper
pubpc 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 );
   }
}

Update CarMapperTest.java with following code −

CarMapperTest.java


package com.tutorialspoint.mapping;

import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.GregorianCalendar;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
import com.tutorialspoint.entity.CarEntity;
import com.tutorialspoint.mapper.CarMapper;
import com.tutorialspoint.model.Car;

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

   @Test
   pubpc 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());
   }
}

Run the following command to test the mappings.


mvn clean test

Output

Once command is successful. Verify the output.


mvn clean test
[INFO] Scanning for projects...
...
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mapping ---
[INFO] Surefire report directory: mvnmapping	argetsurefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.tutorialspoint.mapping.CarMapperTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.035 sec
Running com.tutorialspoint.mapping.DepveryAddressMapperTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec
Running com.tutorialspoint.mapping.StudentMapperTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec

Results :

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
...
Advertisements