BeanUtils.copyProperties使用笔记
编程技术  /  houtizong 发布于 3年前   171
public class User { private Integer intVal; private Double doubleVal; private Short shortVal; private Long longVal; private Float floatVal; private Byte byteVal; private Boolean booleanVal;}User src = new User();User dest = new User();BeanUtils.copyProperties(dest, src);System.out.println(src);System.out.println(dest);//输出User [intVal=null, doubleVal=null, shortVal=null, longVal=null, floatVal=null, byteVal=null, booleanVal=null]User [intVal=0, doubleVal=0.0, shortVal=0, longVal=0, floatVal=0.0, byteVal=0, booleanVal=false]
import org.apache.commons.beanutils.converters.IntegerConverter;IntegerConverter converter = new IntegerConverter(null); //默认为null,而不是0BeanUtilsBean beanUtilsBean = new BeanUtilsBean();beanUtilsBean.getConvertUtils().register(converter, Integer.class);
public class User2 { private java.util.Date javaUtilDateVal; private java.sql.Date javaSqlDateVal; private java.sql.Timestamp javaSqlTimeStampVal; private BigDecimal bigDecimalVal; private java.sql.Time javaSqlTime;}User2 src = new User2();User2 dest = new User2();BeanUtilsBean beanUtilsBean = new BeanUtilsBean();//如果没有下面几行,则在转换null时会抛异常,例如:org.apache.commons.beanutils.ConversionException: No value specified for 'BigDecimal'//在org.apache.commons.beanutils.converters这个包下面有很多的Converter,可以按需要使用beanUtilsBean.getConvertUtils().register(new org.apache.commons.beanutils.converters.BigDecimalConverter(null), BigDecimal.class);beanUtilsBean.getConvertUtils().register(new org.apache.commons.beanutils.converters.DateConverter(null), java.util.Date.class);beanUtilsBean.getConvertUtils().register(new org.apache.commons.beanutils.converters.SqlTimestampConverter(null), java.sql.Timestamp.class);beanUtilsBean.getConvertUtils().register(new org.apache.commons.beanutils.converters.SqlDateConverter(null), java.sql.Date.class);beanUtilsBean.getConvertUtils().register(new org.apache.commons.beanutils.converters.SqlTimeConverter(null), java.sql.Time.class);beanUtilsBean.copyProperties(dest, src);System.out.println(src);System.out.println(dest);
import org.apache.commons.beanutils.BeanUtilsBean;import org.apache.commons.beanutils.PropertyUtils;public class CopyWhenNullBeanUtilsBean extends BeanUtilsBean{ @Override public void copyProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException { try { Object destValue = PropertyUtils.getSimpleProperty(bean, name); if (destValue == null) { super.copyProperty(bean, name, value); } } catch (NoSuchMethodException e) { throw new RuntimeException(e); } }}
import org.apache.commons.beanutils.BeanUtilsBean;public class CopyFromNotNullBeanUtilsBean extends BeanUtilsBean { @Override public void copyProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException { if (value == null) { return; } super.copyProperty(bean, name, value); }}
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接