簡體   English   中英

使用Dozer Mapper API將java.sql.Time映射到整數(毫秒)

[英]Map java.sql.Time to Integer (milliseconds) with Dozer Mapper API

我有2節課

public class A
{
    public java.sql.Time startAt;
}

public class B
{
    public int startAt;
}

如果嘗試映射它,則會收到錯誤消息,無法將時間轉換為整數(自1970年1月1日以來的毫秒數)。 閱讀文檔后,我需要定義客戶轉換器。 我的問題是

  1. 如何使用Dozer API做到這一點
  2. 有沒有辦法將java.sql.Time的所有實例都轉換為Integer? 所以我不需要為每個類定義轉換器?

您可以這樣創建一個自定義類:

import java.util.Date;

import org.dozer.DozerConverter;
import org.joda.time.DateTime;

public class JodaTimeToDateConverter extends DozerConverter<Date, DateTime> {


  public JodaTimeToDateConverter() {
    super(Date.class, DateTime.class);
  }


  @Override
  public DateTime convertTo(Date source, DateTime destination) {
    DateTime result = null;
    if(source != null) {
      result = new DateTime(source.getTime());
    }
    return result;
  }

  @Override
  public Date convertFrom(DateTime source, Date destination) {
    Date result = null;
    if(source != null) {
      result = new Date(source.getMillis());
    }
    return result;
  }



}

然后,將一個configure塊添加到推土機xml中:

<configuration>
      <custom-converters>
            <converter type="foo.bar.CustomConverterClass">
                <class-a>java.util.Date</class-a>
                <class-b>org.joda.time.DateTime</class-b>
            </converter>
    </configuration>

暫無
暫無

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

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