Skip to content

Commit 5d38063

Browse files
Doha2012pivovarit
authored andcommitted
guide to jmapper (eugenp#4745)
* guide to jmapper * Update JMapperIntegrationTest.java * Update JMapperRelationalIntegrationTest.java
1 parent 94eff53 commit 5d38063

12 files changed

Lines changed: 542 additions & 0 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,12 @@
772772
<scope>test</scope>
773773
</dependency>
774774

775+
<dependency>
776+
<groupId>com.googlecode.jmapper-framework</groupId>
777+
<artifactId>jmapper-core</artifactId>
778+
<version>${jmapper.version}</version>
779+
</dependency>
780+
775781
</dependencies>
776782

777783
<repositories>
@@ -1015,6 +1021,7 @@
10151021
<jersey.version>1.19.4</jersey.version>
10161022
<javalin.version>1.6.0</javalin.version>
10171023
<fugue.version>4.5.1</fugue.version>
1024+
<jmapper.version>1.6.0.1</jmapper.version>
10181025
<maven-bundle-plugin.version>3.3.0</maven-bundle-plugin.version>
10191026
<maven-jar-plugin.version>3.0.2</maven-jar-plugin.version>
10201027
</properties>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.jmapper;
2+
3+
import java.time.LocalDate;
4+
5+
6+
public class User {
7+
8+
private long id;
9+
private String email;
10+
private LocalDate birthDate;
11+
12+
// constructors
13+
14+
public User() {
15+
super();
16+
}
17+
18+
public User(long id, String email, LocalDate birthDate) {
19+
super();
20+
this.id = id;
21+
this.email = email;
22+
this.birthDate = birthDate;
23+
}
24+
25+
// getters and setters
26+
27+
public long getId() {
28+
return id;
29+
}
30+
31+
public void setId(long id) {
32+
this.id = id;
33+
}
34+
35+
public String getEmail() {
36+
return email;
37+
}
38+
39+
public void setEmail(String email) {
40+
this.email = email;
41+
}
42+
43+
public LocalDate getBirthDate() {
44+
return birthDate;
45+
}
46+
47+
public void setBirthDate(LocalDate birthDate) {
48+
this.birthDate = birthDate;
49+
}
50+
51+
@Override
52+
public String toString() {
53+
return "User [id=" + id + ", email=" + email + ", birthDate=" + birthDate + "]";
54+
}
55+
56+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.baeldung.jmapper;
2+
3+
import java.time.LocalDate;
4+
import java.time.Period;
5+
6+
import com.googlecode.jmapper.annotations.JMap;
7+
import com.googlecode.jmapper.annotations.JMapConversion;
8+
9+
public class UserDto {
10+
11+
@JMap
12+
private long id;
13+
14+
@JMap("email")
15+
private String username;
16+
17+
@JMap("birthDate")
18+
private int age;
19+
20+
@JMapConversion(from={"birthDate"}, to={"age"})
21+
public int conversion(LocalDate birthDate){
22+
return Period.between(birthDate, LocalDate.now()).getYears();
23+
}
24+
25+
// constructors
26+
27+
public UserDto() {
28+
super();
29+
}
30+
31+
public UserDto(long id, String username, int age) {
32+
super();
33+
this.id = id;
34+
this.username = username;
35+
this.age = age;
36+
}
37+
38+
// getters and setters
39+
40+
public long getId() {
41+
return id;
42+
}
43+
44+
public void setId(long id) {
45+
this.id = id;
46+
}
47+
48+
public String getUsername() {
49+
return username;
50+
}
51+
52+
public void setUsername(String username) {
53+
this.username = username;
54+
}
55+
56+
public int getAge() {
57+
return age;
58+
}
59+
60+
public void setAge(int age) {
61+
this.age = age;
62+
}
63+
64+
@Override
65+
public String toString() {
66+
return "UserDto [id=" + id + ", username=" + username + ", age=" + age + "]";
67+
}
68+
69+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.jmapper;
2+
3+
import com.googlecode.jmapper.annotations.JGlobalMap;
4+
5+
@JGlobalMap
6+
public class UserDto1 {
7+
8+
private long id;
9+
private String email;
10+
11+
12+
// constructors
13+
14+
public UserDto1() {
15+
super();
16+
}
17+
18+
public UserDto1(long id, String email) {
19+
super();
20+
this.id = id;
21+
this.email = email;
22+
}
23+
24+
// getters and setters
25+
26+
public long getId() {
27+
return id;
28+
}
29+
30+
public void setId(long id) {
31+
this.id = id;
32+
}
33+
34+
public String getEmail() {
35+
return email;
36+
}
37+
38+
public void setEmail(String email) {
39+
this.email = email;
40+
}
41+
42+
@Override
43+
public String toString() {
44+
return "UserDto [id=" + id + ", email=" + email + "]";
45+
}
46+
47+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.jmapper.relational;
2+
3+
import com.googlecode.jmapper.annotations.JMap;
4+
5+
6+
public class User {
7+
8+
@JMap(classes = {UserDto1.class, UserDto2.class})
9+
private long id;
10+
11+
@JMap(attributes = {"username", "email"}, classes = {UserDto1.class, UserDto2.class})
12+
private String email;
13+
14+
// constructors
15+
16+
public User() {
17+
super();
18+
}
19+
20+
public User(long id, String email) {
21+
super();
22+
this.id = id;
23+
this.email = email;
24+
}
25+
26+
// getters and setters
27+
28+
public long getId() {
29+
return id;
30+
}
31+
32+
public void setId(long id) {
33+
this.id = id;
34+
}
35+
36+
public String getEmail() {
37+
return email;
38+
}
39+
40+
public void setEmail(String email) {
41+
this.email = email;
42+
}
43+
44+
@Override
45+
public String toString() {
46+
return "User [id=" + id + ", email=" + email + "]";
47+
}
48+
49+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.jmapper.relational;
2+
3+
4+
public class UserDto1 {
5+
6+
private long id;
7+
private String username;
8+
9+
// constructors
10+
11+
public UserDto1() {
12+
super();
13+
}
14+
15+
public UserDto1(long id, String username) {
16+
super();
17+
this.id = id;
18+
this.username = username;
19+
}
20+
21+
// getters and setters
22+
23+
public long getId() {
24+
return id;
25+
}
26+
27+
public void setId(long id) {
28+
this.id = id;
29+
}
30+
31+
public String getUsername() {
32+
return username;
33+
}
34+
35+
public void setUsername(String username) {
36+
this.username = username;
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return "UserDto [id=" + id + ", username=" + username + "]";
42+
}
43+
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.jmapper.relational;
2+
3+
4+
public class UserDto2 {
5+
6+
private long id;
7+
private String email;
8+
9+
// constructors
10+
11+
public UserDto2() {
12+
super();
13+
}
14+
15+
public UserDto2(long id, String email) {
16+
super();
17+
this.id = id;
18+
this.email = email;
19+
}
20+
21+
// getters and setters
22+
23+
public long getId() {
24+
return id;
25+
}
26+
27+
public void setId(long id) {
28+
this.id = id;
29+
}
30+
31+
public String getEmail() {
32+
return email;
33+
}
34+
35+
public void setEmail(String email) {
36+
this.email = email;
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return "UserDto2 [id=" + id + ", email=" + email + "]";
42+
}
43+
44+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<jmapper>
2+
<class name="com.baeldung.jmapper.UserDto">
3+
<attribute name="id">
4+
<value name="id"/>
5+
</attribute>
6+
<attribute name="username">
7+
<value name="email"/>
8+
</attribute>
9+
</class>
10+
</jmapper>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<jmapper>
2+
<class name="com.baeldung.jmapper.UserDto1">
3+
<global/>
4+
</class>
5+
</jmapper>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<jmapper>
2+
<class name="com.baeldung.jmapper.relational.User">
3+
<attribute name="id">
4+
<value name="id"/>
5+
<classes>
6+
<class name="com.baeldung.jmapper.relational.UserDto1"/>
7+
<class name="com.baeldung.jmapper.relational.UserDto2"/>
8+
</classes>
9+
</attribute>
10+
<attribute name="email">
11+
<attributes>
12+
<attribute name="username"/>
13+
<attribute name="email"/>
14+
</attributes>
15+
<classes>
16+
<class name="com.baeldung.jmapper.relational.UserDto1"/>
17+
<class name="com.baeldung.jmapper.relational.UserDto2"/>
18+
</classes>
19+
</attribute>
20+
</class>
21+
</jmapper>

0 commit comments

Comments
 (0)