English 中文(简体)
MapStruct - Implicit Type Conversion
  • 时间:2024-09-17

MapStruct - Imppcit Type Conversion


Previous Page Next Page  

MapStruct handles conversion of type conversions automatically in most of the cases. For example, int to Long or String conversion. Conversion handles null values as well. Following are the some of the important automatic conversions.

    Between primitive types and Corresponding Wrapper Classes.

    Between primitive types and String.

    Between enum types and String.

    Between BigInt, BigDecimal and String.

    Between Calendar/Date and XMLGregorianCalendar.

    Between XMLGregorianCalendar and String.

    Between Jodas date types and String.

Example

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

Update StudentEntity.java with following code −

StudentEntity.java


package com.tutorialspoint.entity;

pubpc class StudentEntity {
   private String id;
   private String name;
   private String classVal;
   private SubjectEntity subject;
   pubpc String section;
   pubpc String getId() {
      return id;
   }
   pubpc void setId(String id) {
      this.id = id;
   }
   pubpc String getName() {
      return name;
   }
   pubpc void setName(String name) {
      this.name = name;
   }
   pubpc String getClassVal() {
      return classVal;
   }
   pubpc void setClassVal(String classVal) {
      this.classVal = classVal;
   }
   pubpc SubjectEntity getSubject() {
      return subject;
   }
   pubpc void setSubject(SubjectEntity subject) {
      this.subject = subject;
   }
}

Student.java is unchanged with following code −

Student.java


package com.tutorialspoint.model;

pubpc class Student {
   private final String name;
   private final int id;

   protected Student(Student.Builder builder) {
      this.name = builder.name;
      this.id = builder.id;
   }
   pubpc static Student.Builder builder() {
      return new Student.Builder();
   }
   pubpc static class Builder {
      private String name;
      private int id;

      pubpc Builder name(String name) {
         this.name = name;
         return this;
      }
      pubpc Builder id(int id) {
         this.id = id;
         return this;
      }
      pubpc Student create() {
         return new Student( this );
      }
   }
   pubpc String getName() {
      return name;
   }
   pubpc int getId() {
      ret+urn id;
   }
}

Update DepveryAddressMapperTest.java with following code −

DepveryAddressMapperTest.java


package com.tutorialspoint.mapping;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
import com.tutorialspoint.entity.AddressEntity;
import com.tutorialspoint.entity.StudentEntity;
import com.tutorialspoint.mapper.DepveryAddressMapper;
import com.tutorialspoint.model.DepveryAddress;

pubpc class DepveryAddressMapperTest {
   private DepveryAddressMapper depveryAddressMapper = Mappers.getMapper(DepveryAddressMapper.class);

   @Test
   pubpc void testEntityToModel() {
      StudentEntity student = new StudentEntity();
      student.setClassVal("X");
      student.setName("John");
      student.setId("1");

      AddressEntity address = new AddressEntity();
      address.setCity("Y");
      address.setState("Z");
      address.setHouseNo(1);

      DepveryAddress depveryAddress = depveryAddressMapper.getDepveryAddress(student, address);

      assertEquals(depveryAddress.getName(), student.getName());
      assertEquals(depveryAddress.getCity(), address.getCity());
      assertEquals(depveryAddress.getState(), address.getState());
      assertEquals(depveryAddress.getHouseNumber(), address.getHouseNo());
   }
}

Update StudentMapperTest.java with following code −

StudentMapperTest.java


package com.tutorialspoint.mapping;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
import com.tutorialspoint.entity.StudentEntity;
import com.tutorialspoint.entity.SubjectEntity;
import com.tutorialspoint.mapper.StudentMapper;
import com.tutorialspoint.model.Student;

pubpc class StudentMapperTest {
   private StudentMapper studentMapper = Mappers.getMapper(StudentMapper.class);

   @Test
   pubpc void testEntityToModel() {
      StudentEntity entity = new StudentEntity();
      entity.setName("John");
      entity.setId("1");

      Student model = studentMapper.getModelFromEntity(entity);
      assertEquals(entity.getName(), model.getName());
      assertEquals(Integer.parseInt(entity.getId()), model.getId());
   }
   @Test
   pubpc void testModelToEntity() {
      Student.Builder builder = Student.builder().id(1).name("John");
      Student model = builder.create();
      StudentEntity entity = studentMapper.getEntityFromModel(model);

      assertEquals(entity.getName(), model.getName());
      assertEquals(Integer.parseInt(entity.getId()), model.getId());
   }
}

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.DepveryAddressMapperTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec
Running com.tutorialspoint.mapping.StudentMapperTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec

Results :

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