Orika简单使用
来源:互联网 发布:硅谷ios程序员 编辑:程序博客网 时间:2024/06/05 08:38
使用orika进行对象间Mapping
1. 当两个类的属性名都一样
package com.orika;/** * Title:CopiedStudent.java * Description: * * @author zhuyang * @version 1.0 2017/7/18 */public class CopiedStudent { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "CopiedStudent{" + "name='" + name + '\'' + ", age=" + age + '}'; }}
package com.orika;/** * Title:Student.java * Description: * * @author zhuyang * @version 1.0 2017/7/18 */public class Student { private Integer age; private String name; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
/** * copy object which filed name is totally same * @param stu source student */ public static void map1(Student stu){ //since filed name is same, so no need to register MapperFactory factory = new DefaultMapperFactory.Builder().build(); CopiedStudent cs = factory.getMapperFacade(Student.class,CopiedStudent.class).map(stu); System.out.println("map1==="+cs); }
2.两个类的字段名不一样
package com.orika;/** * Title:CopiedStudent2.java * Description: * * @author zhuyang * @version 1.0 2017/7/18 */public class CopiedStudent2 { private String name; private Integer copyAge; @Override public String toString() { return "CopiedStudent2{" + "name='" + name + '\'' + ", copyAge=" + copyAge + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getCopyAge() { return copyAge; } public void setCopyAge(Integer copyAge) { this.copyAge = copyAge; }}
/** * copy object which filed name is different * @param stu */ public static void map2(Student stu){ MapperFactory factory = new DefaultMapperFactory.Builder().build(); //since filed name is not same, so register is required ClassMapBuilder builder = factory.classMap(Student.class,CopiedStudent2.class); builder.field("age","copyAge"); builder.byDefault().register(); CopiedStudent2 cs2 = factory.getMapperFacade().map(stu,CopiedStudent2.class); System.out.println("map2==="+cs2); }
3. List拷贝
public static void mapAsList(List<Student> studens){ //register copiedStudent2, since Student and CopiedStudent2 are using different filed name, so register is required. MapperFactory factory = new DefaultMapperFactory.Builder().build(); ClassMapBuilder builder = factory.classMap(Student.class,CopiedStudent2.class); builder.field("age","copyAge");// map field builder.byDefault().register(); List<CopiedStudent2> copiedStudent2s = factory.getMapperFacade().mapAsList(studens,CopiedStudent2.class); copiedStudent2s.forEach(student ->{ System.out.println("copiedStuden2 list = "+student); }); }
阅读全文
1 0
- Orika简单使用
- java关于使用Orika的mapperFactory进行对象拷贝以及集合拷贝
- orika的问题
- Orika User Guide
- Orika映射工具MapperFactory使用方法,及LocalDateTime转换异常
- Springboot集成MapperFactory(ma.glasnost.orika.MapperFactory)类属性复制
- 简单使用
- 简单使用
- 简单使用
- springBoot简单学习使用;curl简单使用
- java使用redis简单使用
- spring 简单使用AOC使用
- logstash使用之简单使用
- 简单易懂的使用
- ant的简单使用
- jUDDI 简单安装使用
- 简单使用有限自动机
- 简单使用_dopostback函数
- 小型抓图爬虫程序实现c++
- solr清除所有索引
- 235. Lowest Common Ancestor of a Binary Search Tree
- JS学习(6)----Math And Random
- MySQL 数据类型
- Orika简单使用
- Jsp页面通过href拼接url向后台传中文乱码问题
- 成为更好开发者的几点建议
- 华科14_2
- JSTL
- 不同编译器对于(a++)*(a++)和(a++)*(++a)和(++a)*(a++)和(++a)*(++a)的运算结果
- [牛客网]错题整理(2)
- java设计模式之单例模式的七种写法
- HDU 3001 Travelling (状态压缩DP +TSP问题)