簡體   English   中英

使用orika進行自定義映射

[英]Custom Mapping with orika

我正在使用Orika Mapper來映射我的源和目標類的字段。

我可以完美地進行一對一的映射。

我在Source類中有兩個字段,如dateOfDeparture和dateOfArrival。

我必須計算這些日期之間的差異,並映射到目的地類中的“travelDuration”字段。

以下是映射器類。

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); 
        }



    }

請建議如何映射(noOfDays,“travelDuration”)。

以下是您的案例:

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); 
    }
}

希望這可以提供幫助。

另一種方法可能是CustomConverter

http://orika-mapper.github.io/orika-docs/converters.html

我做了一些特定的轉換器(List - > List),如:

   ...
   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());
    }
  }
  ... 

我希望這可以幫助那些正在尋找如何轉換這樣的列表的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM