MapperFacade使用
Posted Shinka_YXS
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MapperFacade使用相关的知识,希望对你有一定的参考价值。
引入依赖
<dependency>
<groupId>ma.glasnost.orika</groupId>
<artifactId>orika-core</artifactId>
<version>1.5.4</version>
</dependency>
配置类
import ma.glasnost.orika.MapperFacade;
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 OrikaConfig
@Bean
public MapperFactory mapperFactory()
return new DefaultMapperFactory.Builder().build();
@Bean
public MapperFacade mapperFacade()
return mapperFactory().getMapperFacade();
使用举例
@Autowired
private MapperFacade mapperFacade;
DTO dto = mapperFacade.map(domain, DTO.class);
List<DTO> dtoList = mapperFacade.mapAsList(domain, DTO.class);
报错排查
报错:
An exception occured while compiling the following method:
public void mapAtoB(java.lang.Object a, java.lang.Object b, ma.glasnost.orika.MappingContext mappingContext)
super.mapAtoB(a, b, mappingContext);
// sourceType: ZonedDateTime
java.time.ZonedDateTime source = ((java.time.ZonedDateTime)a);
// destinationType: ZonedDateTime
java.time.ZonedDateTime destination = ((java.time.ZonedDateTime)b);……
for ma.glasnost.orika.generated.Orika_ZonedDateTime_ZonedDateTime_Mapper196016043448600$1
javassist.CannotCompileException: [source error] getChronology() not found in java.time.ZonedDateTime
……
Resolved [ma.glasnost.orika.MappingException: While attempting the following mapping:
sourceType = java.time.ZonedDateTime
destinationType = java.time.ZonedDateTime
Error occurred: ma.glasnost.orika.impl.generator.CompilerStrategy$SourceCodeGenerationException: Error compiling ma.glasnost.orika.generated.Orika_ZonedDateTime_ZonedDateTime_Mapper196016043448600$1]
原因:ZoneDateTime
类型映射异常,具体原因未知,升级版本得以解决(orika-core原本使用的1.5.0,改为1.5.4之后无此异常)、
以上是关于MapperFacade使用的主要内容,如果未能解决你的问题,请参考以下文章