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实现对象复制的更多相关文章

  1. SpringBoot 如何进行对象复制,老鸟们都这么玩的!

    大家好,我是飘渺. 今天带来SpringBoot老鸟系列的第四篇,来聊聊在日常开发中如何优雅的实现对象复制. 首先我们看看为什么需要对象复制? 为什么需要对象复制 如上,是我们平时开发中最常见的三层M ...

  2. PHP基础知识之对象复制

    对象的复制默认为浅复制 进行深复制的方法为:在类中定义魔法方法__clone(),类的对象复制时,会自动调用 __clone方法,在 __clone方法中可以进行各种复制对象的个性化 class My ...

  3. JS对象复制

    在JavaScript很多人复制一个对象的时候都是直接用"=",因为大家都觉得脚本语言是没有指针.引用.地址之类的,所以直接用"="就可以把一个对象复制给另外一 ...

  4. PHP写时复制, 变量复制和对象复制不同!!!

    2016年3月18日 15:09:28 星期五 一直以为PHP对象也是写时复制....... 其实: PHP的变量是写时复制, 对象是引用的 写时复制: $a = $b; 如果$b的内容不改变, $a ...

  5. 【转】JavaScript中的对象复制(Object Clone)

    JavaScript中并没有直接提供对象复制(Object Clone)的方法.因此下面的代码中改变对象b的时候,也就改变了对象a. a = {k1:1, k2:2, k3:3}; b = a; b. ...

  6. 对象复制、克隆、深度clone

    -------------------------------------------------------------------------------- ------------------- ...

  7. PHP5的对象复制

    今天用yii开发程序,一个bug改了一晚上,最后发现问题出在了对象复制机制上,PHP5之前的对象复制只需要$object_a = $object_b即可,但PHP5这样得到的是浅复制,及指针指向,并不 ...

  8. Java反射 - 2(对象复制,父类域,内省)

    为什么要复制对象?假设有个类Car,包含name,color2个属性,那么将car1对象复制给car2对象,只需要car2.setName(car1.getName)与car2.setColor(ca ...

  9. js原生设计模式——7原型模式之真正的原型模式——对象复制封装

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

随机推荐

  1. expect命令自动登录ssh

    expect是简单的工具原因,依赖于tcl. 直接apt安装就行. 四个关键字: spawn,派生出新进程. expect,期待得到的字符串,可以模式匹配. send,向进程发送字符串. intera ...

  2. python第三方库requests简单介绍

    一.发送请求与传递参数 简单demo: import requests r = requests.get(url='http://www.itwhy.org') # 最基本的GET请求 print(r ...

  3. Java06-java基础语法(五)数组

    Java06-java基础语法(五)数组 一.循环的嵌套 在一个循环体内部再含有一个或多个循环 强调:内循环全部做完以后再去执行下一次的外循环 int k = 0; for(int i = 0; i& ...

  4. php中时间转换函数

    date("Y-m-d H:i",$unixtime)  1.php中获得今天零点的时间戳 要获得零点的unix时间戳,可以使用 $todaytime=strtotime(“tod ...

  5. bean生命周期_Junit测试使用factory模式

    在面试某互联网_保险 公司, 被问到了spring中bean的生命周期,不禁联想到我们之前作 junit测试时,使用了Factory模式,而没有用Mock.

  6. mysql 定时任务的使用

    mysql5.1.6增加了一个事件调度器(Event Scheduler),可以做定时任务(定时删除记录,定时数据统计),取代之前系统的计划任务.mysql事件调度器可以精确到每秒执行一个任务. 事件 ...

  7. StackExchange.Redis使用以及封装

    来源:http://www.cnblogs.com/qtqq/p/5951201.html Redis安装:http://www.runoob.com/redis/redis-install.html ...

  8. @JsonView 简单介绍[z]

    @JsonView是jackson json中的一个注解,Spring webmvc也支持这个注解. 这个注解的作用就是控制输入输出后的json. 假设我们有一个用户类,其中包含用户名和密码,一般情况 ...

  9. IDEA安装小配置

    1. view-->toolbar+toolbuttons 2. 根据大小写IDEA能准确提示 配置自动导入包 定义代码模板 提示忽略大小写 配置虚拟机内存,修改idea64.exe.vmopt ...

  10. C# 一段通用的写log 日志的好程序

    public void Write(string text) { FileStream fs = new FileStream(Application.StartupPath+"/log.t ...