2013-01-02 95 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