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框架功能完善 ...
随机推荐
- DDD:架构思想的旧瓶新酒
DDD.DSL 和 DCI DDD 概念最早提出于 2004 年,作为一种软件开发的指导思想,DDD 对软件开发带来了诸多可能与方向,张晓龙认为 DDD 为软件开发带来的好处主要有以下几点: 首先,最 ...
- Spring Security 流程
首先创建4个类 流程大致如下: 1.容器启动 加载系统资源与权限列表(HashMap) MyInvocationSecurityMetadataSourceService中的loadResourceD ...
- CQOI2005 三角形面积并 和 POJ1177 Picture
1845: [Cqoi2005] 三角形面积并 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 1664 Solved: 443[Submit][Stat ...
- Git报错:Please tell me who you are.
Git在提交的时候报错 Please tell me who you are. 报错 Please tell me who you are. 具体如下: 原因:明确报错.请告诉我你是谁.意思是你在提交 ...
- Coins in a Line II
There are n coins with different value in a line. Two players take turns to take one or two coins fr ...
- How To Add User To Sudoers On Debian 10 Buster
In today’s tutorial, we are going to see how you can add a user to sudoers on Debian distributions. ...
- Oracle11g 新特性之动态变量窥视
从11g開始,这个尴尬的问题開始得到了改善.因此从11g開始,引入了所谓的自适应游标共享(Adaptive Cursor Sharing).该特性是一个很复杂的技术,用来平衡游标共享和SQL优化这两个 ...
- RabbitMQ的5种模式
队列截图,去rabbitMq.com去找学习文档 =========================================================================== ...
- 07_Kibana界面操作ES
Kibana界面的API操作ES 1.创建索引 1.1 指定分片数量和备份数量 1.2 创建默认 2. 查看索引 2.1 查看单个索引设置 2.2 查看所有索引设置 3.文档管理 3.1 添加文档 3 ...
- 【HDU4622】Reincarnation
[HDU4622]Reincarnation 一眼似乎不可做,但发现\(strlen(x)\)很小,暴力\(O(n^2)\)预处理每个区间\((l,r)\),查询时\(O(1)\)输出就好了 #inc ...