1. 程式人生 > >BeanUtils 轉換器將Long ,Integer等數字型別的預設值設定為null

BeanUtils 轉換器將Long ,Integer等數字型別的預設值設定為null

  1. static {  
  2.     ConvertUtils.register(new LongConverter(null), Long.class);  
  3.     ConvertUtils.register(new ShortConverter(null), Short.class);  
  4.     ConvertUtils.register(new IntegerConverter(null), Integer.class);  
  5.     ConvertUtils.register(new DoubleConverter(null), Double.class);  
  6.     ConvertUtils.register(new
     BigDecimalConverter(null), BigDecimal.class);  
今天在使用BeanUtils時發現copyProperties方法從Long到Long複製的時候,原欄位為null,結果拷貝過去之後目標欄位變成了0,這不是我想要的結果,在網上搜索了一下,是可以通過轉換器來設定為null時的預設轉換值。
於是我就把一些常用的數字型的欄位的預設轉換值設定成了null,然後再使用BeanUtils.copyProperties拷貝欄位,Long,Integer等欄位為null的就不會預設轉換成0了。