一、简介
Orika是java Bean映射框架,可以实现从一个对象递归拷贝数据至另一个对象。在开发多层应用程序中非常有用。在这些层之间交换数据时,通常为了适应不同API需要转换一个实例至另一个实例。
有很多方法可以实现:硬代码拷贝或Dozer实现bean映射等。总之,需要简化不同层对象之间映射过程。
Orika使用字节码生成器创建开销最小的快速映射,比其他基于反射方式实现(如,Dozer)更快。
二、配置实用
2.1 引入依赖包
<dependency>
<groupId>ma.glasnost.orika</groupId>
<artifactId>orika-core</artifactId>
<version>1.4.6</version>
</dependency>
读者可以查找最新版本。
2.2 orika配置
配置MapperFactoryBean
package com.****.fsd.ums.common.config.orika;
import com.hikvision.fsd.ums.common.config.date.LocalDateTimeToLongConverter;
import ma.glasnost.orika.CustomConverter;
import ma.glasnost.orika.Mapper;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import ma.glasnost.orika.metadata.ClassMapBuilder;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class MapperFactoryBean implements FactoryBean<MapperFactory>, ApplicationContextAware {
ApplicationContext applicationContext;
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public MapperFactory getObject() throws Exception {
DefaultMapperFactory build = new DefaultMapperFactory.Builder().build();
for (CustomConverter converter : applicationContext.getBeansOfType(CustomConverter.class).values()) {
build.getConverterFactory().registerConverter(converter);
}
for (Mapper<?, ?> mapper : applicationContext.getBeansOfType(Mapper.class).values()) {
build.registerMapper(mapper);
}
for (ClassMapBuilder<?, ?> mapper : applicationContext.getBeansOfType(ClassMapBuilder.class).values()) {
build.registerClassMap(mapper);
}
return build;
}
@Override
public Class<?> getObjectType() {
return MapperFactory.class;
}
@Override
public boolean isSingleton() {
return true;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
配置OrikaConfig
package com.****.fsd.ums.common.config.orika;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.converter.BidirectionalConverter;
import ma.glasnost.orika.metadata.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
@Configuration
public class OrikaConfig {
@Bean
public MapperFactoryBean loadFactory(){
return new MapperFactoryBean();
}
@Bean
public MapperFacade loadMapperFacade(MapperFactory factory){
return factory.getMapperFacade();
}
@Autowired
private MapperFactory mapperFactory;
/**
* 解决orika映射LocalDateTime报错问题
*/
@PostConstruct
public void init() {
mapperFactory.getConverterFactory().registerConverter(new LocalDateTimeConverter());
mapperFactory.getConverterFactory().registerConverter(new LocalDateConverter());
mapperFactory.getConverterFactory().registerConverter(new LocalTimeConverter());
}
private class LocalDateTimeConverter extends BidirectionalConverter<LocalDateTime, LocalDateTime> {
@Override
public LocalDateTime convertTo(LocalDateTime source, Type<LocalDateTime> destinationType) {
return LocalDateTime.from(source);
}
@Override
public LocalDateTime convertFrom(LocalDateTime source, Type<LocalDateTime> destinationType) {
return LocalDateTime.from(source);
}
}
private class LocalDateConverter extends BidirectionalConverter<LocalDate, LocalDate> {
@Override
public LocalDate convertTo(LocalDate source, Type<LocalDate> destinationType) {
return LocalDate.from(source);
}
@Override
public LocalDate convertFrom(LocalDate source, Type<LocalDate> destinationType) {
return LocalDate.from(source);
}
}
private class LocalTimeConverter extends BidirectionalConverter<LocalTime, LocalTime> {
@Override
public LocalTime convertTo(LocalTime source, Type<LocalTime> destinationType) {
return LocalTime.from(source);
}
@Override
public LocalTime convertFrom(LocalTime source, Type<LocalTime> destinationType) {
return LocalTime.from(source);
}
}
}
3 基本使用
@Autowired
private MapperFacade mapperFacade;
mapperFacade.map(sourceObj, targetObj.class);
mapperFacade.mapAsList(sourceList, targetObj.class);
高级使用参考:
https://blog.csdn.net/neweastsun/article/details/80559868