2013-01-02 91 views

回答

3

從文檔的粗略瀏覽,你可以通過自定義映射BeanMapping幾乎任何東西任何東西,所以......「是的」

+0

但是,它是可行的與C語言相同的規則自動轉換? – jmpb

+0

是 - 你會寫一些簡單的像'回報,我= 0' – Bohemian

1

是....你可以整型映射到布爾或任何其他數據類型。對於這種映射的需要Custom Converters

1
public class NewDozerConverter 
    extends DozerConverter<Integer, Boolean> { 

    public NewDozerConverter() { 
    super(Integer.class, Boolean.class); 
    } 

    public Boolean convertTo(Integer source, Boolean destination) { 
    if (source > 1) { 
     return Boolean.TRUE; 
    } else if (source < 0) { 
     return Boolean.FALSE; 
    } 
    throw new IllegalStateException("Unknown value!"); 
    } 

    public Integer convertFrom(Boolean source, Integer destination) { 
    if (Boolean.TRUE.equals(source)) { 
     return 1; 
    } else if (Boolean.FALSE.equals(source)) { 
     return 0; 
    } 
    throw new IllegalStateException("Unknown value!"); 
    } 

} 
+0

'的ConvertTo(整數源,布爾目的地)'應該考慮的情況下源整數等於1或0更新代碼: ' ! public Boolean convertTo(Integer source,Boolean destination)if(source> = 1)返回Boolean.TRUE; else if(source <= 0){ return Boolean.FALSE; } throw new IllegalStateException(「Unknown value!」); } ' – gammay

+0

此代碼充滿了錯誤。 :)首先,「來源」在兩個函數中都可能爲空。其次,convertTo(...)爲由convertFrom(...)返回的兩個值中的任何一個引發異常!也就是說,convertTo(convertFrom(Boolean.TRUE,null))拋出IllegalStateException。兩種方法都不需要使用「目標」。這是無用的,因爲無論如何這個方法都不能改變這個值。最後,convertTo(Integer val)應該是{return null!= val && val!= 0}。 – laloumen

0

如果您只需要0和1分別映射到虛實,它已經被推土機外的開箱處理。如果您想將0映射爲false並將任何其他值映射爲true,則需要一個custom converter