orika 深拷贝

orika 深拷贝

1
2
3
4
5
<dependency>
<groupId>ma.glasnost.orika</groupId>
<artifactId>orika-core</artifactId>
<version>1.5.4</version>
</dependency>

定义Account对象

1
2
3
4
5
6
@Data
public class Account {
private long id;
private String username;
private String password;
}

定义Friend

1
2
3
4
5
@Data
public class Friend {
private String a;
private String b;
}

定义UserA对象

1
2
3
4
5
6
7
8
9
10
11
@Data
public class UserA {
private long id;
private String name;
private int age;
private Account account;

private List<Friend> friends;

private String a;
}

定义UserB对象

1
2
3
4
5
6
7
8
9
10
11
@Data
public class UserB {
private long id;
private String name;
private int age;
private Account account;

private List<Friend> friends;

private String b;
}

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
@SpringBootTest
class OrikaApplicationTests {

@Test
void contextLoads() {

Account account = new Account();
account.setId(1);
account.setUsername("zhangsan");
account.setPassword("zhangsan");

Friend friend1 = new Friend();
friend1.setA("a");
friend1.setB("a");
Friend friend2 = new Friend();
friend2.setA("b");
friend2.setB("b");
List<Friend> friendListA = new ArrayList<>();
friendListA.add(friend1);
friendListA.add(friend2);

UserA userA = new UserA();
userA.setId(1);
userA.setName("张三");
userA.setAge(18);
userA.setAccount(account);
userA.setFriends(friendListA);
userA.setA("A");

UserB userB = new UserB();
List<Friend> friendListB = new ArrayList<>();
Friend friend3 = new Friend();
friend3.setA("c");
friend3.setB("c");
friendListB.add(friend3);
userB.setFriends(friendListB);

System.out.println(userA);
System.out.println(userB);

//转换
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
MapperFacade mapper = mapperFactory.getMapperFacade();
// UserB userB = mapper.map(userA, UserB.class);
mapper.map(userA, userB);
System.out.println(userA);
System.out.println(userB);
}

}

orika 深拷贝
https://happyloves.cn/20220831/47e907f6ff52.html
作者
赵小胖
发布于
2022年8月31日
许可协议