Java 使用 orika 自定义映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24445201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom Mapping with orika
提问by user67867
I am using Orika Mapper for mapping my fields of source and destination class.
我正在使用 Orika Mapper 来映射我的源和目标类的字段。
I can do one-to-one mapping perfectly.
我可以完美地进行一对一映射。
I have two fields like dateOfDeparture and dateOfArrival in Source class.
我在 Source 类中有两个字段,如 dateOfDeparture 和 dateOfArrival。
I have to calculate difference between these dates and map to a field "travelDuration" in destination class.
我必须计算这些日期之间的差异并映射到目标类中的“travelDuration”字段。
Following is the mapper class.
以下是映射器类。
package com.tcs.Orika;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.converter.ConverterFactory;
import ma.glasnost.orika.impl.ConfigurableMapper;
import ma.glasnost.orika.impl.DefaultMapperFactory;
public class Mapper extends ConfigurableMapper {
public static void main(String[] args) throws ParseException {
/*Date Calculation*/
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
String dateOfDeparture = "31-08-1982 10:20:56";
String dateOfArrival = "31-08-1983 10:20:56";
Date date1 = dateFormat.parse(dateOfDeparture);
Date date2 = dateFormat.parse(dateOfArrival);
long diff = date2.getTime() - date1.getTime();
int noOfDays = (int) TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
/*set values to Source object*/
OrikaMapFrom objectMapFrom = new OrikaMapFrom();
OrikaMapTo objectMapTo = new OrikaMapTo();
objectMapFrom.setSource("Delhi");
objectMapFrom.setDestination("Amsterdam");
objectMapFrom.setDateOfDeparture(date1);
objectMapFrom.setDateOfArrival(date2);
/*Name Mapping when source and destination names are different*/
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapperFactory.classMap(OrikaMapFrom.class, OrikaMapTo.class)
.field("source", "sourcePlace")
.field("destination","destinationPlace")
.field(noOfDays,"travelDuration")--------------->facing error on this line
.register();
/*Value Mapping*/
MapperFacade mapper = mapperFactory.getMapperFacade();
objectMapTo = mapper.map(objectMapFrom, OrikaMapTo.class);
objectMapTo.setTravelDuration(noOfDays);
System.out.println(objectMapTo.getSourcePlace());
System.out.println(objectMapTo.getDestinationPlace());
System.exit(0);
}
}
please Advice how to map (noOfDays,"travelDuration").
请建议如何映射(noOfDays,“travelDuration”)。
采纳答案by Sidi
Here is the way for your case:
这是您的情况的方法:
public class Mapper {
public static void main(String[] args) throws ParseException {
/*Date Calculation*/
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
String dateOfDeparture = "31-08-1982 10:20:56";
String dateOfArrival = "31-08-1983 10:20:56";
Date date1 = dateFormat.parse(dateOfDeparture);
Date date2 = dateFormat.parse(dateOfArrival);
/*set values to Source object*/
OrikaMapFrom objectMapFrom = new OrikaMapFrom();
OrikaMapTo objectMapTo = new OrikaMapTo();
objectMapFrom.setSource("Delhi");
objectMapFrom.setDestination("Amsterdam");
objectMapFrom.setDateOfDeparture(date1);
objectMapFrom.setDateOfArrival(date2);
/*Name Mapping when source and destination names are different*/
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapperFactory.classMap(OrikaMapFrom.class, OrikaMapTo.class)
.field("source", "sourcePlace")
.field("destination","destinationPlace")
.customize(new CustomMapper<OrikaMapFrom,OrikaMapTo>() {
@Override
public void mapAtoB(OrikaMapFrom a, OrikaMapTo b, MappingContext mappingContext) {
long diff = a.getDateOfArrival().getTime() - a.getDateOfDeparture().getTime();
b.setTravelDuration((int)TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
}
})
//.field(noOfDays,"travelDuration")--------------->facing error on this line
.register();
/*Value Mapping*/
MapperFacade mapper = mapperFactory.getMapperFacade();
objectMapTo = mapper.map(objectMapFrom, OrikaMapTo.class);
objectMapTo.setTravelDuration(noOfDays);
System.out.println(objectMapTo.getSourcePlace());
System.out.println(objectMapTo.getDestinationPlace());
System.exit(0);
}
}
Hope this can help.
希望这能有所帮助。
回答by cesaregb
Another way could be with the CustomConverter
另一种方法可能是使用 CustomConverter
http://orika-mapper.github.io/orika-docs/converters.html
http://orika-mapper.github.io/orika-docs/converters.html
I made some specific converter (List -> List) like:
我制作了一些特定的转换器(列表 - > 列表),例如:
...
ConverterFactory converterFactory = BaseMapper.MAPPER_FACTORY.getConverterFactory();
converterFactory.registerConverter("orderListConverter", new OrderListConverter());
BaseMapper.MAPPER_FACTORY.classMap(ClientDTO.class, Client.class)
.fieldMap("orders", "orders").converter("orderListConverter").mapNulls(true).mapNullsInReverse(true).add()
.byDefault()
.register();
mapperFacade = BaseMapper.MAPPER_FACTORY.getMapperFacade();
...
class OrderListConverter extends BidirectionalConverter<List<Order>, List<Integer>> {
@Override
public List<Order> convertFrom(List<Integer> source, Type<List<Order>> destT) {
return source.stream().map(p -> (new Order()).setIdOrder(p)).collect(Collectors.toList());
}
@Override
public List<Integer> convertTo(List<Order> source, Type<List<Integer>> destT) {
return source.stream().map(p -> p.getIdOrder()).collect(Collectors.toList());
}
}
...
I hope this helps someone that is looking on how to convert a list like that.
我希望这对正在研究如何转换这样的列表的人有所帮助。