Springboot集成MapperFactory(ma.glasnost.orika.MapperFactory)类属性复制
一、导入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)类属性复制的更多相关文章
- springboot集成redis使用redis作为session报错ClassNotFoundException类RememberMeServices
springboot 集成redis使用redis作为缓存,会报错的问题. 错误信息: java.lang.IllegalStateException: Error processing condit ...
- 【springBoot】springBoot集成redis的key,value序列化的相关问题
使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...
- springboot集成Actuator
Actuator监控端点,主要用来监控与管理. 原生端点主要分为三大类:应用配置类.度量指标类.操作控制类. 应用配置类:获取应用程序中加载的配置.环境变量.自动化配置报告等与SpringBoot应用 ...
- SpringBoot集成redis的key,value序列化的相关问题
使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...
- springboot集成mybatis(二)
上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...
- springboot集成mybatis(一)
MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...
- springboot集成redis(mybatis、分布式session)
安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...
- SpringBoot集成jsp
一.springBoot集成jsp: 1.修改pom文件 <!--集成jsp所需jar包--> <!--jsp页面使用jstl标签--> <dependency> ...
- springboot集成schedule(深度理解)
背景 在项目开发过程中,我们经常需要执行具有周期性的任务.通过定时任务可以很好的帮助我们实现. 我们拿常用的几种定时任务框架做一个比较: 从以上表格可以看出,Spring Schedule框架功能完善 ...
随机推荐
- Socket实现client和server端通信(Java)(转)
转自: https://blog.csdn.net/yayun0516/article/details/50819147 https://www.jianshu.com/p/2d4f223f1462 ...
- U-Net网络的Pytorch实现
1.文章原文地址 U-Net: Convolutional Networks for Biomedical Image Segmentation 2.文章摘要 普遍认为成功训练深度神经网络需要大量标注 ...
- P1281 书的复制[二分]
题目描述 现在要把m本有顺序的书分给k给人复制(抄写),每一个人的抄写速度都一样,一本书不允许给两个(或以上)的人抄写,分给每一个人的书,必须是连续的,比如不能把第一.第三.第四本书给同一个人抄写. ...
- vue2 运动及相关函数
- 牛客练习赛33 E. tokitsukaze and Similar String (字符串哈希)
题目链接:https://ac.nowcoder.com/acm/contest/308/E 题意:中文题 见链接 题解:哈希预处理(三哈希模板) #include <bits/stdc++.h ...
- 如何下载oracle jdk|oracle jdk下载慢,要登录等等问题
wget -c --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup ...
- Python tkinter 实现简单登陆注册 基于B/S三层体系结构,实现用户身份验证
Python tkinter 实现简单登陆注册 最终效果 开始界面 注册 登陆 源码 login.py # encoding=utf-8 from tkinter import * from ...
- 什么是webpack模块化构建工具
百度百科模块化:是指解决一个复杂问题时自顶向下逐层把系统划分成若干模块的过程,有多种属性,分别反映其内部特性. 计算机模块化:一般指的是可以被抽象封装的最小/最优代码集合,模块化解决的是功能耦合问题. ...
- HTML 003 元素
HTML 元素 HTML 文档由 HTML 元素定义. HTML 元素 开始标签 * 元素内容 结束标签 * <p> 这是一个段落 </p> <a href=" ...
- Spring AOP的作用,动态代理模式
AOP即面向切面编程.AOP是基于代理模式的. 代理模式: 当我们需要修改一个类,在类中加入代码时,为了不破坏这个类的封装性.可以使用代理模式,建立一个代理类. 比如:修改需求,在调用UserCont ...