1、新建maven工程orika-demo,引入orika依赖,其中pom.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>orika.demo</groupId>
<artifactId>orika-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>ma.glasnost.orika</groupId>
<artifactId>orika-core</artifactId>
<version>1.4.1</version>
</dependency>
</dependencies>
</project>
2、测试场景一:类名不同,属性完全相同的复制
public class UserDTO {
private String name;
private String sex; public UserDTO(String name, String sex) {
this.name = name;
this.sex = sex;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
} @Override
public String toString() {
return "UserDTO{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
public class UserDO {
private String name;
private String sex; public UserDO(String name, String sex) {
this.name = name;
this.sex = sex;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
} @Override
public String toString() {
return "UserDO{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
public class Test {
public static void main(String[] args){
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
UserDTO userDTO = new UserDTO("张三","男");
UserDO userDO = mapperFactory.getMapperFacade().map(userDTO,UserDO.class);
System.out.println(userDO.toString());
}
}
UserDO{name='张三', sex='男'}
3、测试场景二:类名不同,部分属性不同的复制
public class UserDTO {
private String name;
private String sex; public UserDTO(String name, String sex) {
this.name = name;
this.sex = sex;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
} @Override
public String toString() {
return "UserDTO{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
public class NewUserDTO {
private String name;
private String newSex; public NewUserDTO(String name, String newSex) {
this.name = name;
this.newSex = newSex;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getNewSex() {
return newSex;
} public void setNewSex(String newSex) {
this.newSex = newSex;
} @Override
public String toString() {
return "NewUserDTO{" +
"name='" + name + '\'' +
", newSex='" + newSex + '\'' +
'}';
}
}
public class Test {
public static void main(String[] args){
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
UserDTO userDTO = new UserDTO("张三","男");
//如果不使用byDefault,则只会拷贝被field配置的属性,最后使用register使其生效
mapperFactory.classMap(UserDTO.class,NewUserDTO.class).field("sex", "newSex").byDefault().register();
NewUserDTO newUserDTO = mapperFactory.getMapperFacade().map(userDTO,NewUserDTO.class);
System.out.println(newUserDTO.toString());
}
}
NewUserDTO{name='张三', newSex='男'}
4、测试场景三:集合复制,集合内类的属性名相同
public class UserDTO {
private String name;
private String sex; public UserDTO(String name, String sex) {
this.name = name;
this.sex = sex;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
} @Override
public String toString() {
return "UserDTO{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
public class UserDO {
private String name;
private String sex; public UserDO(String name, String sex) {
this.name = name;
this.sex = sex;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
} @Override
public String toString() {
return "UserDO{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
public class Test {
public static void main(String[] args){
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
List<UserDTO> userDTOList = new ArrayList<UserDTO>();
userDTOList.add(new UserDTO("张三","男"));
userDTOList.add(new UserDTO("李英","女"));
List<UserDO> userDOList = mapperFactory.getMapperFacade().mapAsList(userDTOList,UserDO.class);
System.out.println(userDOList.toString());
}
}
[UserDO{name='张三', sex='男'}, UserDO{name='李英', sex='女'}]
5、测试场景四:集合复制,集合内类的属性名部分不同
public class UserDTO {
private String name;
private String sex; public UserDTO(String name, String sex) {
this.name = name;
this.sex = sex;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
} @Override
public String toString() {
return "UserDTO{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
public class NewUserDTO {
private String name;
private String newSex; public NewUserDTO(String name, String newSex) {
this.name = name;
this.newSex = newSex;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getNewSex() {
return newSex;
} public void setNewSex(String newSex) {
this.newSex = newSex;
} @Override
public String toString() {
return "NewUserDTO{" +
"name='" + name + '\'' +
", newSex='" + newSex + '\'' +
'}';
}
}
public class Test {
public static void main(String[] args){
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
List<UserDTO> userDTOList = new ArrayList<UserDTO>();
userDTOList.add(new UserDTO("张三","男"));
userDTOList.add(new UserDTO("李英","女"));
mapperFactory.classMap(UserDTO.class, NewUserDTO.class).field("sex","newSex").byDefault().register();
List<NewUserDTO> newUserDTOList = mapperFactory.getMapperFacade().mapAsList(userDTOList,NewUserDTO.class);
System.out.println(newUserDTOList.toString());
}
}
[NewUserDTO{name='张三', newSex='男'}, NewUserDTO{name='李英', newSex='女'}]
6、测试场景五:类复制,类里面包含集合属性,类的属性部分不同,集合属性名字相同,但是集合里面的类属性部分不同
public class UserListDTO {
private String listName;
private List<UserDTO> userList; public String getListName() {
return listName;
} public void setListName(String listName) {
this.listName = listName;
} public List<UserDTO> getUserList() {
return userList;
} public void setUserList(List<UserDTO> userList) {
this.userList = userList;
} @Override
public String toString() {
return "UserListDTO{" +
"listName='" + listName + '\'' +
", userList=" + userList +
'}';
}
}
public class NewUserListDTO {
private String newListName;
private List<NewUserDTO> userList; public String getNewListName() {
return newListName;
} public void setNewListName(String newListName) {
this.newListName = newListName;
} public List<NewUserDTO> getUserList() {
return userList;
} public void setUserList(List<NewUserDTO> userList) {
this.userList = userList;
} @Override
public String toString() {
return "NewUserListDTO{" +
"newListName='" + newListName + '\'' +
", userList=" + userList +
'}';
}
}
public class Test {
public static void main(String[] args){
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
List<UserDTO> userDTOList = new ArrayList<UserDTO>();
userDTOList.add(new UserDTO("张三","男"));
userDTOList.add(new UserDTO("李英","女"));
UserListDTO userListDTO = new UserListDTO();
userListDTO.setListName("list001");
userListDTO.setUserList(userDTOList);
mapperFactory.classMap(UserDTO.class, NewUserDTO.class).field("sex","newSex").byDefault().register();
mapperFactory.classMap(UserListDTO.class, NewUserListDTO.class).field("listName","newListName").byDefault().register();
NewUserListDTO newUserListDTO = mapperFactory.getMapperFacade().map(userListDTO,NewUserListDTO.class);
System.out.println(newUserListDTO.toString());
}
}
NewUserListDTO{newListName='list001', userList=[NewUserDTO{name='张三', newSex='男'}, NewUserDTO{name='李英', newSex='女'}]}
orika实现对象复制的更多相关文章
-
SpringBoot 如何进行对象复制,老鸟们都这么玩的!
大家好,我是飘渺. 今天带来SpringBoot老鸟系列的第四篇,来聊聊在日常开发中如何优雅的实现对象复制. 首先我们看看为什么需要对象复制? 为什么需要对象复制 如上,是我们平时开发中最常见的三层M ...
-
PHP基础知识之对象复制
对象的复制默认为浅复制 进行深复制的方法为:在类中定义魔法方法__clone(),类的对象复制时,会自动调用 __clone方法,在 __clone方法中可以进行各种复制对象的个性化 class My ...
-
JS对象复制
在JavaScript很多人复制一个对象的时候都是直接用"=",因为大家都觉得脚本语言是没有指针.引用.地址之类的,所以直接用"="就可以把一个对象复制给另外一 ...
-
PHP写时复制, 变量复制和对象复制不同!!!
2016年3月18日 15:09:28 星期五 一直以为PHP对象也是写时复制....... 其实: PHP的变量是写时复制, 对象是引用的 写时复制: $a = $b; 如果$b的内容不改变, $a ...
-
【转】JavaScript中的对象复制(Object Clone)
JavaScript中并没有直接提供对象复制(Object Clone)的方法.因此下面的代码中改变对象b的时候,也就改变了对象a. a = {k1:1, k2:2, k3:3}; b = a; b. ...
-
对象复制、克隆、深度clone
-------------------------------------------------------------------------------- ------------------- ...
-
PHP5的对象复制
今天用yii开发程序,一个bug改了一晚上,最后发现问题出在了对象复制机制上,PHP5之前的对象复制只需要$object_a = $object_b即可,但PHP5这样得到的是浅复制,及指针指向,并不 ...
-
Java反射 - 2(对象复制,父类域,内省)
为什么要复制对象?假设有个类Car,包含name,color2个属性,那么将car1对象复制给car2对象,只需要car2.setName(car1.getName)与car2.setColor(ca ...
-
js原生设计模式——7原型模式之真正的原型模式——对象复制封装
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
随机推荐
-
使用getopt_long来解析参数的小函数模板
getopt_long原型 #define no_argument 0 #define required_argument 1 #define optional_argument 2 struct o ...
-
BZOJ4377 : [POI2015]Kurs szybkiego czytania
因为$a$与$n$互质,所以对于$0$到$n-1$里每个$i$,$ai\bmod n$的值互不相同. 设匹配成功的起点为$i$,那么可以得到$3m$段$ai\bmod n$的值不能取的禁区,每段都是连 ...
-
Numpy系列(三)- 基本运算操作
Numpy 中数组上的算术运算符使用元素级别.最后的结果使用新的一个数组来返回. import numpy as np a = np.array( [20,30,40,50] ) b = np.ara ...
-
7.6.2 break 语句
7.6.2 break 语句 程序执行到循环中的break语句时,会种植包含它的循环,并继续执行下一阶段. 如果break语句位于嵌套循环内,它只会影响包含它的当前循环. break还可用于因其他原因 ...
-
BZOJ.3307.雨天的尾巴(dsu on tree/线段树合并)
BZOJ 洛谷 \(dsu\ on\ tree\).(线段树合并的做法也挺显然不写了) 如果没写过\(dsu\)可以看这里. 对修改操作做一下差分放到对应点上,就成了求每个点子树内出现次数最多的颜色, ...
-
POJ 3169.Layout 最短路
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11612 Accepted: 5550 Descripti ...
-
【最小割】【网络流24题】【P2762】 太空飞行计划问题
Description W 教授正在为国家航天中心计划一系列的太空飞行.每次太空飞行可进行一系列商业性实验而获取利润.现已确定了一个可供选择的实验集合E={E1,E2,-,Em},和进行这些实验需要使 ...
-
node.js和npm离线安装
离线安装node.js和npm 1.下载官方安装包并拷贝到离线机器上. 官方下载地址:https://nodejs.org/en/download/ 2.解压文件: tar-xJf node-v8.9 ...
-
python使用hbase
#coding:utf-8 __author__ = 'similarface' from multiprocessing import Process import happybase import ...
-
python__高级 : Property 的使用
一个类中,假如一个私有属性,有两个方法,一个是getNum , 一个是setNum 它,那么可以用 Property 来使这两个方法结合一下,比如这样用 num = property(getNum, ...