Merge pull request #6268 from amit2103/BAEL-12114

[BAEL-12114] - Upgraded MapStruct article
This commit is contained in:
Loredana Crusoveanu 2019-02-03 09:58:35 +02:00 committed by GitHub
commit 8a9aba5d89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 182 additions and 1 deletions

View File

@ -30,6 +30,11 @@
<version>${springframework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</dependency>
</dependencies>
<build>
@ -48,6 +53,11 @@
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
@ -55,10 +65,11 @@
</build>
<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>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<org.projectlombok.version>1.18.4</org.projectlombok.version>
</properties>
</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());
}
}