beanutils.copyproperties,beanutilscopyproperties 拷贝集合

http://www.itjxue.com  2023-01-07 12:47  来源:未知  点击次数: 

如何实现BeanUtils.copyProperties方法的功能

第一步: BeanUtils.copyProperties()与PropertyUtils.copyProperties() 1、 通过反射将一个对象的值赋值个另外一个对象(前提是对象中属性的名字相同)。 2、 BeanUtils.copyProperties(obj1,obj2); 经常闹混不知道是谁给谁赋值,无意中先到"后

如何实现beanutils.copyproperties不复制某些字段

/**

* 复制属性,过滤掉不复制的属性

*/

public static void copyBeanProperties(

final Object source,//1,待复制的原始对象

final Object target,//2,复制后的结果对象

//3,获取保存你不需要复制的属性名

final CollectionString excludes = new ArrayListString();

final PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(source.getClass());

for(final PropertyDescriptor propertyDescriptor : propertyDescriptors){

String propName = propertyDescriptor.getName();

if(!includes.contains(propName)){

excludes.add(propName);

}

}

//4,复制操作

BeanUtils.copyProperties(source, target, excludes.toArray(new String[excludes.size()]));

}

BeanUtils.copyProperties拷贝form到实体bean时出错

java.lang.IllegalArgumentException: argument type mismatch。看字面意思就是参数类型不匹配啊,你的表单全是String,而实体bean却有int有date。

可以把bean的属性改成String, 或者把表单里的某些值改下类型。

-----------新回复:

你的Date是java.util.Date吗?貌似不支持这个,需要改成java.sql.Date。

BeanUtils支持的转换类型如下: * java.lang.BigDecimal * java.lang.BigInteger * boolean and java.lang.Boolean * byte and java.lang.Byte * char and java.lang.Character * java.lang.Class * double and java.lang.Double * float and java.lang.Float * int and java.lang.Integer * long and java.lang.Long * short and java.lang.Short * java.lang.String * java.sql.Date * java.sql.Time * java.sql.Timestamp 这里要注意一点,java.util.Date是不被支持的,而它的子类java.sql.Date是被支持的。因此如果对象包含时间类型的属性,且希望被转换的时候,一定要使用java.sql.Date类型。否则在转换时会提示argument mistype异常。

BeanUtils.copyProperties无法拷贝问题

项目中我们经常会调用第三方接口,对于第三方的返回数据 DTO 需要使用到原型模式 转换为前端所需要的 VO ,对于数据结构不变化的我们经常使用的 BeanUtils.copyProperties 来实现转换。

问题: ADto 中包含 BDto 拷贝数据给 AVo 中包含 BVo 会出现 BDto 无法拷贝 BVo 现象

原因: BeanUtils.copyProperties 是通过 类型 和 名称 进行拷贝查找的,当BDto 与 BVo 数据类型 不一致时候是无法拷贝的。

BeanUtils.copyProperties 选择性赋值字段

先创建一个实体类

在赋制数据

控制台输出如下数据,说明数据赋制成功。

在工作中不要全部赋制数据,需要有选择性赋制字段。 比如有三个字段 user1 , user2 , user3。

要将user1 的 name 值和user2 的 age 值赋值给user3。

BeanUtils.copyProperties 提供了忽略字段接口 ,源码如下:

以下是排除规则,如果字段为空,字段就不赋值。

实现上面的 user1,user2 的字段全都复制给 user3。

控制台输出如下数据,说明赋值成功。

Hibernate的BeanUtils.copyProperties方法,怎么不能拷贝对象

第一步: BeanUtils.copyProperties()与PropertyUtils.copyProperties()

1、 通过反射将一个对象的值赋值个另外一个对象(前提是对象中属性的名字相同)。

2、 BeanUtils.copyProperties(obj1,obj2); 经常闹混不知道是谁给谁赋值,无意中先到"后付前"这个词来帮助自己记忆这个功能。即将obj2的值赋值给obj1。

3、 如果2中实例obj2为空对象,即值new了他的实例并没有赋值的话obj1对应的属性值也会被设置为空置。

4、BeanUtils与PropertyUtils对比(这里对比copyProperties方法)

PropertyUtils的copyProperties()方法几乎与BeanUtils.copyProperties()相同,主要的区别在于后者提供类型转换功能,即发现两个JavaBean的同名属性为不同类型时,在支持的数据类型范围内进行转换,BeanUtils 不支持这个功能,但是BeanUtils速度会更快一些。

主要支持转换类型如下:

* java.lang.BigDecimal

* java.lang.BigInteger

* boolean and java.lang.Boolean

* byte and java.lang.Byte

* char and java.lang.Character

* java.lang.Class

* double and java.lang.Double

* float and java.lang.Float

* int and java.lang.Integer

* long and java.lang.Long

* short and java.lang.Short

* java.lang.String

* java.sql.Date

* java.sql.Time

* java.sql.Timestamp

不支持java.util.Date转换,但支持java.sql.Date。如果开发中Date类型采用util而非sql.Date程序会抛出argument mistype异常。

第二步:扩展BeanUtils支持时间类型转换

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;

import org.apache.commons.beanutils.ConvertUtils;

/**

* 重写BeanUtils.copyProperties

*

* @author monkey

*/

public class BeanUtilsExtends extends BeanUtils {

static {

ConvertUtils.register(new DateConvert(), java.util.Date.class);

ConvertUtils.register(new DateConvert(), java.sql.Date.class);

}

public static void copyProperties(Object dest, Object orig) {

try {

BeanUtils.copyProperties(dest, orig);

} catch (IllegalAccessException ex) {

ex.printStackTrace();

} catch (InvocationTargetException ex) {

ex.printStackTrace();

}

}

}

import java.text.ParseException;

import java.text.SimpleDateFormat;

import org.apache.commons.beanutils.Converter;

/**

* 重写日期转换

*

* @author houzhiqing

*/

public class DateConvert implements Converter {

public Object convert(Class arg0, Object arg1) {

String p = (String) arg1;

if (p == null || p.trim().length() == 0) {

return null;

}

try {

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

return df.parse(p.trim());

} catch (Exception e) {

try {

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

return df.parse(p.trim());

} catch (ParseException ex) {

return null;

}

}

}

}

(责任编辑:IT教学网)

更多

相关3DMAX教程文章

推荐3DMAX教程文章