Skip to content

Commit 1f7bbd3

Browse files
committed
[BAEL-12114] - Upgraded MapStruct article
1 parent e37e543 commit 1f7bbd3

File tree

9 files changed

+182
-1
lines changed

9 files changed

+182
-1
lines changed

mapstruct/pom.xml

+12-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
<version>${springframework.version}</version>
3131
<scope>test</scope>
3232
</dependency>
33+
<dependency>
34+
<groupId>org.projectlombok</groupId>
35+
<artifactId>lombok</artifactId>
36+
<version>${org.projectlombok.version}</version>
37+
</dependency>
3338
</dependencies>
3439

3540
<build>
@@ -48,17 +53,23 @@
4853
<artifactId>mapstruct-processor</artifactId>
4954
<version>${org.mapstruct.version}</version>
5055
</path>
56+
<path>
57+
<groupId>org.projectlombok</groupId>
58+
<artifactId>lombok</artifactId>
59+
<version>${org.projectlombok.version}</version>
60+
</path>
5161
</annotationProcessorPaths>
5262
</configuration>
5363
</plugin>
5464
</plugins>
5565
</build>
5666

5767
<properties>
58-
<org.mapstruct.version>1.1.0.Final</org.mapstruct.version>
68+
<org.mapstruct.version>1.3.0.Beta2</org.mapstruct.version>
5969
<springframework.version>4.3.4.RELEASE</springframework.version>
6070
<maven.compiler.source>1.8</maven.compiler.source>
6171
<maven.compiler.target>1.8</maven.compiler.target>
72+
<org.projectlombok.version>1.18.4</org.projectlombok.version>
6273
</properties>
6374

6475
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.dto;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
@Getter
7+
@Setter
8+
public class CarDTO {
9+
private int id;
10+
private String name;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.dto;
2+
3+
public class PersonDTO {
4+
5+
private String id;
6+
private String name;
7+
8+
public PersonDTO() {
9+
10+
}
11+
12+
public PersonDTO(String id, String name) {
13+
super();
14+
this.id = id;
15+
this.name = name;
16+
}
17+
18+
public String getId() {
19+
return id;
20+
}
21+
22+
public void setId(String id) {
23+
this.id = id;
24+
}
25+
26+
public String getName() {
27+
return name;
28+
}
29+
30+
public void setName(String name) {
31+
this.name = name;
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.entity;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
@Getter
7+
@Setter
8+
public class Car {
9+
private int id;
10+
private String name;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.entity;
2+
3+
public class Person {
4+
5+
private String id;
6+
private String name;
7+
8+
public Person() {
9+
10+
}
11+
12+
public Person(String id, String name) {
13+
super();
14+
this.id = id;
15+
this.name = name;
16+
}
17+
18+
public String getId() {
19+
return id;
20+
}
21+
22+
public void setId(String id) {
23+
this.id = id;
24+
}
25+
26+
public String getName() {
27+
return name;
28+
}
29+
30+
public void setName(String name) {
31+
this.name = name;
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.mapper;
2+
3+
import org.mapstruct.Mapper;
4+
import org.mapstruct.factory.Mappers;
5+
6+
import com.baeldung.dto.CarDTO;
7+
import com.baeldung.entity.Car;
8+
9+
@Mapper
10+
public interface CarMapper {
11+
12+
CarMapper INSTANCE = Mappers.getMapper(CarMapper.class);
13+
14+
CarDTO carToCarDTO(Car car);
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.mapper;
2+
3+
import org.mapstruct.Mapper;
4+
import org.mapstruct.Mapping;
5+
import org.mapstruct.factory.Mappers;
6+
7+
import com.baeldung.dto.PersonDTO;
8+
import com.baeldung.entity.Person;
9+
10+
@Mapper
11+
public interface PersonMapper {
12+
13+
PersonMapper INSTANCE = Mappers.getMapper(PersonMapper.class);
14+
15+
@Mapping(target = "id", source = "person.id", defaultExpression = "java(java.util.UUID.randomUUID().toString())")
16+
PersonDTO personToPersonDTO(Person person);
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.mapper;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
import com.baeldung.dto.CarDTO;
8+
import com.baeldung.entity.Car;
9+
10+
public class CarMapperUnitTest {
11+
12+
@Test
13+
public void givenCarEntitytoCar_whenMaps_thenCorrect() {
14+
15+
Car entity = new Car();
16+
entity.setId(1);
17+
entity.setName("Toyota");
18+
19+
CarDTO carDto = CarMapper.INSTANCE.carToCarDTO(entity);
20+
21+
assertEquals(carDto.getId(), entity.getId());
22+
assertEquals(carDto.getName(), entity.getName());
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.mapper;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertNull;
6+
7+
import org.junit.Test;
8+
9+
import com.baeldung.dto.PersonDTO;
10+
import com.baeldung.entity.Person;
11+
12+
public class PersonMapperUnitTest {
13+
14+
@Test
15+
public void givenPersonEntitytoPersonWithExpression_whenMaps_thenCorrect() {
16+
17+
Person entity = new Person();
18+
entity.setName("Micheal");
19+
20+
PersonDTO personDto = PersonMapper.INSTANCE.personToPersonDTO(entity);
21+
22+
assertNull(entity.getId());
23+
assertNotNull(personDto.getId());
24+
assertEquals(personDto.getName(), entity.getName());
25+
}
26+
}

0 commit comments

Comments
 (0)