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

MapStruct - Mapping List


Previous Page Next Page  

Using Mapstruct we can map pst in similar fashion as we map primitives. To get a pst of objects, we should provide a mapper method which can map an object.

Syntax


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

Following example demonstrates the same.

Example

Open project mapping as updated in Mapping Using defaultExpression 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;
   private String name;
   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;
   }
   pubpc String getName() {
      return name;
   }
   pubpc void setName(String name) {
      this.name = name;
   }
}

Update Car.java with following code −

Car.java


package com.tutorialspoint.model;

pubpc class Car {
   private int id;
   private String price;
   private String manufacturingDate;
   private String brand;
   private String name;
   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;
   }
   pubpc String getBrand() {
      return brand;
   }
   pubpc void setBrand(String brand) {
      this.brand = brand;
   }
   pubpc String getName() {
      return name;
   }
   pubpc void setName(String name) {
      this.name = name;
   }
}

Update CarMapper.java with following code −

CarMapper.java


package com.tutorialspoint.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import com.tutorialspoint.entity.CarEntity;
import com.tutorialspoint.model.Car;
import java.util.List;
import java.util.UUID;

@Mapper( imports = UUID.class )
pubpc 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> pstOfIntegers);
   List<Car> getCars(List<CarEntity> carEntities);
}

Update CarMapperTest.java with following code −

CarMapperTest.java


package com.tutorialspoint.mapping;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.util.Arrays;
import java.util.GregorianCalendar;
import java.util.List;
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));

      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> pst = Arrays.asList(1,2,3);
      List<String> pstOfStrings = carMapper.getListOfStrings(pst);
      List<Car> pstOfCars = carMapper.getCars(carEntities);

      assertEquals(3, pstOfStrings.size());
      assertEquals(2, pstOfCars.size());
   }
}

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