BeanUtils 转换器将Long ,Integer等字段的默认值设置为null

来源:互联网 发布:淘宝特卖网如何上商品 编辑:程序博客网 时间:2024/05/22 06:34
今天在使用BeanUtils时发现copyProperties方法从Long到Long复制的时候,原字段为null,结果拷贝过去之后目标字段变成了0,这不是我想要的结果,在网上搜索了一下,是可以通过转换器来设置为null时的默认转换值。
static {ConvertUtils.register(new LongConverter(null), Long.class);ConvertUtils.register(new ShortConverter(null), Short.class);ConvertUtils.register(new IntegerConverter(null), Integer.class);ConvertUtils.register(new DoubleConverter(null), Double.class);ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);}

于是我就把一些常用的数字型的字段的默认转换值设置成了null,然后再使用BeanUtils.copyProperties拷贝字段,Long,Integer等字段为null的就不会默认转换成0了。