Skip to content

Commit e479614

Browse files
committed
added persistence
1 parent 7450955 commit e479614

23 files changed

Lines changed: 472 additions & 61 deletions

mapstruct/pom.xml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0"?>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>mapstruct</artifactId>
7+
<name>mapstruct</name>
8+
<groupId>com.baeldung</groupId>
9+
<version>1.0</version>
10+
<packaging>jar</packaging>
11+
12+
<properties>
13+
<org.mapstruct.version>1.0.0.Final</org.mapstruct.version>
14+
</properties>
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.mapstruct</groupId>
18+
<artifactId>mapstruct-jdk8</artifactId>
19+
<version>${org.mapstruct.version}</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>junit</groupId>
23+
<artifactId>junit</artifactId>
24+
<version>4.12</version>
25+
<scope>test</scope>
26+
</dependency>
27+
</dependencies>
28+
<build>
29+
<finalName>mapstruct</finalName>
30+
<plugins>
31+
<plugin>
32+
<groupId>org.apache.maven.plugins</groupId>
33+
<artifactId>maven-compiler-plugin</artifactId>
34+
<version>3.5.1</version>
35+
<configuration>
36+
<source>1.8</source>
37+
<target>1.8</target>
38+
<annotationProcessorPaths>
39+
<path>
40+
<groupId>org.mapstruct</groupId>
41+
<artifactId>mapstruct-processor</artifactId>
42+
<version>${org.mapstruct.version}</version>
43+
</path>
44+
</annotationProcessorPaths>
45+
</configuration>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.baeldung.dto;
2+
3+
public class EmployeeDTO {
4+
5+
private int employeeId;
6+
private String employeeName;
7+
private int divisionId;
8+
private String divisionName;
9+
10+
public int getEmployeeId() {
11+
return employeeId;
12+
}
13+
14+
public void setEmployeeId(int employeeId) {
15+
this.employeeId = employeeId;
16+
}
17+
18+
public String getEmployeeName() {
19+
return employeeName;
20+
}
21+
22+
public void setEmployeeName(String employeeName) {
23+
this.employeeName = employeeName;
24+
}
25+
26+
public int getDivisionId() {
27+
return divisionId;
28+
}
29+
30+
public void setDivisionId(int divisionId) {
31+
this.divisionId = divisionId;
32+
}
33+
34+
public String getDivisionName() {
35+
return divisionName;
36+
}
37+
38+
public void setDivisionName(String divisionName) {
39+
this.divisionName = divisionName;
40+
}
41+
42+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.baeldung.dto;
2+
3+
public class SimpleSource {
4+
5+
private String name;
6+
private String description;
7+
8+
public String getName() {
9+
return name;
10+
}
11+
12+
public void setName(String name) {
13+
this.name = name;
14+
}
15+
16+
public String getDescription() {
17+
return description;
18+
}
19+
20+
public void setDescription(String description) {
21+
this.description = description;
22+
}
23+
24+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.baeldung.entity;
2+
3+
public class Division {
4+
5+
public Division() {
6+
}
7+
8+
public Division(int id, String name) {
9+
super();
10+
this.id = id;
11+
this.name = name;
12+
}
13+
14+
private int id;
15+
private String name;
16+
17+
public int getId() {
18+
return id;
19+
}
20+
21+
public void setId(int id) {
22+
this.id = id;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.baeldung.entity;
2+
3+
public class Employee {
4+
5+
private int id;
6+
private String name;
7+
private Division division;
8+
9+
public int getId() {
10+
return id;
11+
}
12+
13+
public void setId(int id) {
14+
this.id = id;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public void setName(String name) {
22+
this.name = name;
23+
}
24+
25+
public Division getDivision() {
26+
return division;
27+
}
28+
29+
public void setDivision(Division division) {
30+
this.division = division;
31+
}
32+
33+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.baeldung.entity;
2+
3+
public class SimpleDestination {
4+
5+
private String name;
6+
private String description;
7+
8+
public String getName() {
9+
return name;
10+
}
11+
12+
public void setName(String name) {
13+
this.name = name;
14+
}
15+
16+
public String getDescription() {
17+
return description;
18+
}
19+
20+
public void setDescription(String description) {
21+
this.description = description;
22+
}
23+
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.baeldung.mapper;
2+
3+
import org.baeldung.dto.EmployeeDTO;
4+
import org.baeldung.entity.Employee;
5+
import org.mapstruct.Mapper;
6+
import org.mapstruct.Mapping;
7+
import org.mapstruct.Mappings;
8+
9+
@Mapper
10+
public interface EmployeeMapper {
11+
12+
@Mappings({
13+
@Mapping(target="divisionId",source="entity.division.id"),
14+
@Mapping(target="divisionName",source="entity.division.name"),
15+
@Mapping(target="employeeId",source="entity.id"),
16+
@Mapping(target="employeeName",source="entity.name")
17+
})
18+
EmployeeDTO employeeToEmployeeDTO(Employee entity);
19+
20+
@Mappings({
21+
@Mapping(target="id",source="dto.employeeId"),
22+
@Mapping(target="name",source="dto.employeeName"),
23+
@Mapping(target="division",expression="java(new org.baeldung.entity.Division(dto.getDivisionId(),dto.getDivisionName()))")
24+
})
25+
Employee employeeDTOtoEmployee(EmployeeDTO dto);
26+
27+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.baeldung.mapper;
2+
3+
import org.baeldung.dto.SimpleSource;
4+
import org.baeldung.entity.SimpleDestination;
5+
import org.mapstruct.Mapper;
6+
7+
@Mapper
8+
public interface SimpleSourceDestinationMapper {
9+
10+
SimpleDestination sourceToDestination(SimpleSource source);
11+
SimpleSource destinationToSource(SimpleDestination destination);
12+
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.baeldung.mapper;
2+
3+
import org.baeldung.dto.SimpleSource;
4+
import org.baeldung.entity.SimpleDestination;
5+
import org.mapstruct.Mapper;
6+
7+
@Mapper(componentModel="spring")
8+
public interface SimpleSourceDestinationSpringMapper {
9+
10+
SimpleDestination sourceToDestination(SimpleSource source);
11+
SimpleSource destinationToSource(SimpleDestination destination);
12+
13+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.baeldung.mapper;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.baeldung.dto.EmployeeDTO;
6+
import org.baeldung.entity.Division;
7+
import org.baeldung.entity.Employee;
8+
import org.junit.Test;
9+
import org.mapstruct.factory.Mappers;
10+
11+
public class EmployeeMapperTest {
12+
13+
@Test
14+
public void givenEmployeeDTOtoEmployee_whenMaps_thenCorrect(){
15+
EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class);
16+
17+
EmployeeDTO dto = new EmployeeDTO();
18+
dto.setDivisionId(1);
19+
dto.setDivisionName("IT Division");
20+
dto.setEmployeeId(1);
21+
dto.setEmployeeName("John");
22+
23+
Employee entity = mapper.employeeDTOtoEmployee(dto);
24+
25+
assertEquals(dto.getDivisionId(), entity.getDivision().getId());
26+
assertEquals(dto.getDivisionName(), entity.getDivision().getName());
27+
assertEquals(dto.getEmployeeId(),entity.getId());
28+
assertEquals(dto.getEmployeeName(),entity.getName());
29+
}
30+
31+
@Test
32+
public void givenEmployeetoEmployeeDTO_whenMaps_thenCorrect(){
33+
EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class);
34+
35+
Employee entity = new Employee();
36+
entity.setDivision(new Division(1,"IT Division"));
37+
entity.setId(1);
38+
entity.setName("John");
39+
40+
EmployeeDTO dto = mapper.employeeToEmployeeDTO(entity);
41+
42+
assertEquals(dto.getDivisionId(), entity.getDivision().getId());
43+
assertEquals(dto.getDivisionName(), entity.getDivision().getName());
44+
assertEquals(dto.getEmployeeId(),entity.getId());
45+
assertEquals(dto.getEmployeeName(),entity.getName());
46+
}
47+
48+
}

0 commit comments

Comments
 (0)