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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值