[BAEL-12114] - Upgraded MapStruct article

This commit is contained in:
amit2103 2019-02-03 11:55:43 +05:30
parent e37e543976
commit 1f7bbd3c22
9 changed files with 182 additions and 1 deletions

View File

@ -30,6 +30,11 @@
<version>${springframework.version}</version> <version>${springframework.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -48,6 +53,11 @@
<artifactId>mapstruct-processor</artifactId> <artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version> <version>${org.mapstruct.version}</version>
</path> </path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
</annotationProcessorPaths> </annotationProcessorPaths>
</configuration> </configuration>
</plugin> </plugin>
@ -55,10 +65,11 @@
</build> </build>
<properties> <properties>
<org.mapstruct.version>1.1.0.Final</org.mapstruct.version> <org.mapstruct.version>1.3.0.Beta2</org.mapstruct.version>
<springframework.version>4.3.4.RELEASE</springframework.version> <springframework.version>4.3.4.RELEASE</springframework.version>
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.target>1.8</maven.compiler.target>
<org.projectlombok.version>1.18.4</org.projectlombok.version>
</properties> </properties>
</project> </project>

View File

@ -0,0 +1,11 @@
package com.baeldung.dto;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class CarDTO {
private int id;
private String name;
}

View File

@ -0,0 +1,33 @@
package com.baeldung.dto;
public class PersonDTO {
private String id;
private String name;
public PersonDTO() {
}
public PersonDTO(String id, String name) {
super();
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.entity;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Car {
private int id;
private String name;
}

View File

@ -0,0 +1,33 @@
package com.baeldung.entity;
public class Person {
private String id;
private String name;
public Person() {
}
public Person(String id, String name) {
super();
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.mapper;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import com.baeldung.dto.CarDTO;
import com.baeldung.entity.Car;
@Mapper
public interface CarMapper {
CarMapper INSTANCE = Mappers.getMapper(CarMapper.class);
CarDTO carToCarDTO(Car car);
}

View File

@ -0,0 +1,17 @@
package com.baeldung.mapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import com.baeldung.dto.PersonDTO;
import com.baeldung.entity.Person;
@Mapper
public interface PersonMapper {
PersonMapper INSTANCE = Mappers.getMapper(PersonMapper.class);
@Mapping(target = "id", source = "person.id", defaultExpression = "java(java.util.UUID.randomUUID().toString())")
PersonDTO personToPersonDTO(Person person);
}

View File

@ -0,0 +1,24 @@
package com.baeldung.mapper;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.baeldung.dto.CarDTO;
import com.baeldung.entity.Car;
public class CarMapperUnitTest {
@Test
public void givenCarEntitytoCar_whenMaps_thenCorrect() {
Car entity = new Car();
entity.setId(1);
entity.setName("Toyota");
CarDTO carDto = CarMapper.INSTANCE.carToCarDTO(entity);
assertEquals(carDto.getId(), entity.getId());
assertEquals(carDto.getName(), entity.getName());
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.mapper;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import com.baeldung.dto.PersonDTO;
import com.baeldung.entity.Person;
public class PersonMapperUnitTest {
@Test
public void givenPersonEntitytoPersonWithExpression_whenMaps_thenCorrect() {
Person entity = new Person();
entity.setName("Micheal");
PersonDTO personDto = PersonMapper.INSTANCE.personToPersonDTO(entity);
assertNull(entity.getId());
assertNotNull(personDto.getId());
assertEquals(personDto.getName(), entity.getName());
}
}