orika实现对象复制
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 ...
随机推荐
- selenimu学习二
1.上传文件 from selenium import webdriver import time import os driver = webdriver.Chrome() src_file = & ...
- Nginx中超时时间配置(转)
本文介绍 Nginx 的 超时(timeout)配置.分享给大家,具体如下: Nginx 处理的每个请求均有相应的超时设置.如果做好这些超时时间的限定,判定超时后资源被释放,用来处理其他的请求,以此提 ...
- XSS 攻击的防御
xss攻击预防,网上有很多介绍,发现很多都是只能预防GET方式请求的xss攻击,并不能预防POST方式的xss攻击.主要是由于POST方式的参数只能用流的方式读取,且只能读取一次,经过多次尝试,自己总 ...
- mysql --secure-file-priv is set to NULL.Operations related to importing and exporting data are disabled
--secure-file-priv is set to NULL. Operations related to importing and exporting data are disabledmy ...
- document.write : 什么是在html输出中使用,什么是文档加载后使用?
理解:您只能在 HTML 输出中使用 document.write.如果您在文档加载后使用该方法,会覆盖整个文档. Javascript中只能在 HTML 输出流中使用 document.write, ...
- AsyncTask的缺陷
导语:在开发Android应用的过程中,我们需要时刻注意保障应用的稳定性和界面响应性,因为不稳定或者响应速度慢的应用将会给用户带来非常差的交互体验.在越来越讲究用户体验的大环境下,用户也许会因为应用的 ...
- Liunx Pwd
Linux中用 pwd 命令来查看”当前工作目录“的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd来判定当前目录在文件系统内的确切位置. ...
- (转)在JAVA实现DataTable对象(三)——DataTable对象实现
大家都是行家,我就直接上代码了,我这个代码应该还是能看懂的,嘻嘻…. 1: import java.util.ArrayList; 2: import java.util.List; 3: 6: ...
- Activity2.java
package com.hanqi.text3; import android.app.Activity; import android.os.Bundle; import android.os.Pe ...
- 如何使用queue_delayed_work函数
本文转自如何使用queue_delayed_work函数 1. delayed_workqueue主要用在需要延迟处理任务的驱动中,这些驱动的特性主要是不能使用中断. delayed_workqueu ...