一、Orika的MapperFacade
效率:因为Orika的设计思路是预先通过javaassist把JavaBean之间的映射关系一次性生成目标拷贝方法代码。 这样就可以避免在Bean 映射环节一次次的读取映射规则。 从而实现效率上的提高。
这里以Springboot集成MapperFactory为实例。
maven包引入
官网:http://orika-mapper.github.io/orika-docs/intro.html
<dependency>
<groupId>ma.glasnost.orika</groupId>
<artifactId>orika-core</artifactId>
</dependency>
基础概念
MapperFactory
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
MapperFactory用于注册字段映射,配置转换器,自定义映射器等,而我们关注的主要是字段映射这个特性,在下面的小节中会介绍。
MapperFacade
MapperFacade mapper = mapperFactory.getMapperFacade();
PersonSource source = new PersonSource();
PersonDest destination = mapper.map(source, PersonDest.class);
MapperFacade和spring,apache中的BeanUtils具有相同的地位,负责对象间的映射,也是实际使用中,我们使用的最多的类。
springBoot中的使用
编写容器注入的类
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MapperFacotoryAutoWire {
@Bean
public MapperFactory getFactory(){
return new DefaultMapperFactory.Builder().build();
}
映射代码
@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;
}
实战映射方便代码:
//先遍历数组插入:
List<T> touTiaoAdPlacementDataList = response..stream().map(jrttProjectPromotionReportInfo -> jrttNewPlaceMentConverter.convertFrom(jrttProjectPromotionReportInfo).collect(Collectors.toList());
@Component
public class JRTTNewPlaceMentConverter {
private final MapperFactory mapperFactory;
public JRTTNewPlaceMentConverter(MapperFactory mapperFactory) {
this.mapperFactory = mapperFactory;
mapperFactory.classMap(T, TouTiaoAdPlacementData.class)
.field("statCost", "statCost")
.field("showTimes", "showCnt")
.field("clickTimes", "clickCnt")
.byDefault().register();
}
public T convertFrom(T t) {
TouTiaoAdPlacementData touTiaoAdPlacementData = mapperFactory.getMapperFacade().map(T, TouTiaoAdPlacementData.class);
return touTiaoAdPlacementData;
}
}
二、使用BeanUtils.copyProperties进行对象之间的属性赋值
@RunWith(PowerMockRunner.class)
public class TestUtil
{
@Test
public void test(){
Employee ee1=new Employee("A",21,"it");
Employee ee2=new Employee("B",23,"account");
User user=new User();
BeanUtil.copyProperties(ee1, user);
System.out.println(user);
System.out.println("-------------分割线--------------");
List<User> output=new ArrayList<>();
List<Employee> source= Arrays.asList(ee1,ee2);
output=BeanUtil.convertList2List(source,User.class);
for (User str:output) {
System.out.println(str);
}
}
}
三、使用 BeanMapper.copy进行对象之间的属性赋值
List<Student> studentList=new ArrayList<>();
for(Person person in personList){
Student student=new Student();
BeanMapper.copy(person,student);//第一个参数是源数据,第二个参数是目标数据
student.setSexCN(SexEnum.GetDes(person.sex.ToString()));
}