一、导入Jar()

gradle方式

compile group: 'ma.glasnost.orika', name: 'orika-core', version: '1.5.1'

maven方式

<groupId>ma.glasnost.orika</groupId>
<artifactId>orika-core</artifactId>
<version>1.5.1</version>

二、编写容器注入的类

package com.kingboy.springboot.config;

import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /** * @Author kingboy * @Date 2017/4/12 17:44 * @Description MapperFacotoryAutoWire is used to 属性操作工具 */
@Configuration
public class MapperFacotoryAutoWire { @Bean
public MapperFactory getFactory(){
return new DefaultMapperFactory.Builder().build();
} }

三、使用

准备工作

package com.kingboy.springboot.domain;

import java.time.LocalDateTime;
import java.util.Date; /** * @Author kingboy * @Date 2017/4/13 18:32 * @Description Person is used to stupid person */
public class Person { public Person() {
} public Person(String name, Integer age, Date dateTime) {
this.name = name;
this.age = age;
this.dateTime = dateTime;
} private String name; private Integer age; private Date dateTime; public String getName() {
return name;
} public Person setName(String name) {
this.name = name;
return this;
} public Integer getAge() {
return age;
} public Person setAge(Integer age) {
this.age = age;
return this;
} public Date getDateTime() {
return dateTime;
} public Person setDateTime(Date dateTime) {
this.dateTime = dateTime;
return this;
} @Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", dateTime=" + dateTime +
'}';
}
} package com.kingboy.springboot.domain; import java.time.LocalDateTime;
import java.util.Date; /** * @Author kingboy * @Date 2017/4/13 18:33 * @Description Student is used to student */
public class Student { private String name; private String grade; private Integer age; private Date birth; public Date getBirth() {
return birth;
} public Student setBirth(Date birth) {
this.birth = birth;
return this;
} public String getName() {
return name;
} public Student setName(String name) {
this.name = name;
return this;
} public String getGrade() {
return grade;
} public Student setGrade(String grade) {
this.grade = grade;
return this;
} public Integer getAge() {
return age;
} public Student setAge(Integer age) {
this.age = age;
return this;
} @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", grade='" + grade + '\'' +
", age=" + age +
", birth=" + birth +
'}';
}
}

使用方式

package com.kingboy.test;

import com.kingboy.springboot.KingBoyApplication;
import com.kingboy.springboot.domain.Person;
import com.kingboy.springboot.domain.Student;
import com.kingboy.springboot.repository.CityRepository;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.metadata.ClassMapBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; /** * @Author kingboy * @Date 2017/4/13 18:35 * @Description MapperFactoryTest is used to MapperFactory */
@SpringBootTest(classes = KingBoyApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
public class MapperFactoryTest { @Autowired
private MapperFactory mapperFactory; /** * 将一个已经存在的类的属性映射到另外一个类上(可以不存在),直接返回该类,注意必须要有默认构造方法,不然报错 */
@Test
public void copyBeanToBean(){
Person person = new Person("king", 123, new Date());
mapperFactory.classMap(Person.class, Student.class)
.field("dateTime","birth")//不一样的字段映射
.byDefault()//剩余的字段映射
.register();
//如果所有字段一样,则不用写mapperFactory.classMap()方法;
Student student = mapperFactory.getMapperFacade().map(person, Student.class);
System.out.println(student);
//Student{name='king', grade='null', age=123, birth=Thu Apr 13 19:04:43 CST 2017}
} /** * 将一个List映射到另一个List */ @Test
public void copyListToList(){
List<Person> personList = getPersonList();
//手动配置不一样的属性转换
mapperFactory.classMap(Person.class, Student.class)
.field("dateTime","birth")//不一样的字段映射
.byDefault()//剩余的字段映射
.register();
//转换List
List<Student> students = mapperFactory.getMapperFacade().mapAsList(personList, Student.class);
students.forEach(student -> {
System.out.println(student);
});
/** * Student{name='king1', grade='null', age=1, birth=Thu Apr 13 19:10:39 CST 2017} *Student{name='king2', grade='null', age=2, birth=Thu Apr 13 19:10:39 CST 2017} *Student{name='king3', grade='null', age=3, birth=Thu Apr 13 19:10:39 CST 2017} *Student{name='king4', grade='null', age=4, birth=Thu Apr 13 19:10:39 CST 2017} *Student{name='king5', grade='null', age=5, birth=Thu Apr 13 19:10:39 CST 2017} */
} public List<Person> getPersonList(){
List<Person> list = new ArrayList<>(5);
Person person1 = new Person("king1", 1, new Date());
Person person2 = new Person("king2", 2, new Date());
Person person3 = new Person("king3", 3, new Date());
Person person4 = new Person("king4", 4, new Date());
Person person5 = new Person("king5", 5, new Date());
list.add(person1);
list.add(person2);
list.add(person3);
list.add(person4);
list.add(person5);
return list;
}
}

Springboot集成MapperFactory(ma.glasnost.orika.MapperFactory)类属性复制的更多相关文章

  1. springboot集成redis使用redis作为session报错ClassNotFoundException类RememberMeServices

    springboot 集成redis使用redis作为缓存,会报错的问题. 错误信息: java.lang.IllegalStateException: Error processing condit ...

  2. 【springBoot】springBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  3. springboot集成Actuator

    Actuator监控端点,主要用来监控与管理. 原生端点主要分为三大类:应用配置类.度量指标类.操作控制类. 应用配置类:获取应用程序中加载的配置.环境变量.自动化配置报告等与SpringBoot应用 ...

  4. SpringBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  5. springboot集成mybatis(二)

    上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...

  6. springboot集成mybatis(一)

    MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...

  7. springboot集成redis(mybatis、分布式session)

    安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...

  8. SpringBoot集成jsp

    一.springBoot集成jsp: 1.修改pom文件 <!--集成jsp所需jar包--> <!--jsp页面使用jstl标签--> <dependency> ...

  9. springboot集成schedule(深度理解)

    背景 在项目开发过程中,我们经常需要执行具有周期性的任务.通过定时任务可以很好的帮助我们实现. 我们拿常用的几种定时任务框架做一个比较: 从以上表格可以看出,Spring Schedule框架功能完善 ...

随机推荐

  1. MyBatis 3.0_[tp-24-25]_映射文件_参数处理_#与$取值区别_#{}更丰富的用法

    笔记要点出错分析与总结 /**================Mybatis参数值的获取:#和$符号的区别=============== * #{}:可以获得map中的值或者pojo对象属性的值; * ...

  2. 【PAT-二叉树】L2-011. 玩转二叉树- 仅仅开100大的数组模拟即可!

    L2-011. 玩转二叉树 给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列.所谓镜面反转,是指将所有非叶结点的左右孩子对换.(我的分析:无非就是说把左子树当成 ...

  3. P1005 矩阵取数游戏[区间dp]

    题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的\(m*n\)的矩阵,矩阵中的每个元素\(a_{i,j}\)均为非负整数.游戏规则如下: 每次取数时须从每行各取走一个元素,共n个.经过m次后 ...

  4. mac下解决中文乱码的问题

    最近在做自己的博客项目,需要在公司和家代码同步,当然代码托管到了码云上面,但是公司是win系统,家里是mac系统,奇怪的是在公司.....算了不废话了,直接正题吧. 就是代码在公司中文显示没毛病,然后 ...

  5. vue 过滤

  6. Selenium常用API的使用java语言之14-多窗口切换

    在页面操作过程中有时候点击某个链接会弹出新的窗口, 这时就需要主机切换到新打开的窗口上进行操作.WebDriver提供了switchTo().window()方法可以实现在不同的窗口之间切换. 以百度 ...

  7. django安装tinymce

    1. pip install django-tinymce 2. 运行:python manage.py collectstatic 3. 编辑 settings.py 4. TINYMCE_JS_U ...

  8. 文件操作中file.seek()方法

    摘要: file.seek()可以将文件游标移动到文件的任意位置,本文具体的file.seek()文件游标移动操作方法. file.seek()方法标准格式是:seek(offset,whence=0 ...

  9. 什么是 restful 规范?

    一种软件架构风格.设计风格,而不是标准,只是提供了一组设计原则和约束条件. 一.协议 API与用户的通信协议,总是使用HTTPs协议. 什么是https协议 二.域名 应该尽量将API部署在专用域名之 ...

  10. jquery关于多个显示隐藏

    今天做了一个关于多个栏目的隐藏与显示,内容为初始化显示6个栏目,点击按钮显示所有的栏目,在次点击隐藏出现的栏目 <div class="ftlt_z_navigation acer&q ...