gradle方式java
compile group: 'ma.glasnost.orika', name: 'orika-core', version: '1.5.1'
maven方式web
<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(); } }
準備工做spring
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; } }