BeanUtils中copyProperties的使用
来源:互联网 发布:置乱算法 arnold 编辑:程序博客网 时间:2024/06/05 04:30
BeanUtils中copyProperties的作用是将一个对象中的属性值赋值(拷贝)给另一个对象中对应的属性,并且对象之间可以没有任何联系。其中赋值成功的属性对应的属性名和属性类型必须相同,否则对应的属性值不会从一个对象赋值给另一个对象,但是此时不影响其他属性值的拷贝。具体如下所示。
1、实体类
public class Model01 { private String name; private int age; private String city; @Override public String toString() { return "Model01{" + "name='" + name + '\'' + ", age=" + age + ", city='" + city + '\'' + '}'; }}
public class Model02 { private String name; private String age; private String city; @Override public String toString() { return "Model02{" + "name='" + name + '\'' + ", age='" + age + '\'' + ", city='" + city + '\'' + '}'; }}
2、将符合条件的属性值全部从一个对象赋值给另一个对象——copyProperties(Object source, Object target)
public class App { public static void main( String[] args ) { Model01 model01 = new Model01(); Model02 model02 = new Model02(); model01.setName("jack66"); model01.setAge(12); model01.setCity("gz"); model02.setName("henry"); model02.setAge("32"); model02.setCity("sz"); BeanUtils.copyProperties(model01,model02); System.out.println(model01); System.out.println(model02); }}打印结果
Model01{name='jack66', age=12, city='gz'}Model02{name='jack66', age='32', city='gz'}
3、忽略某些属性的赋值——copyProperties(Object source, Object target, String... ignoreProperties)
public class App { public static void main( String[] args ) { Model01 model01 = new Model01(); Model02 model02 = new Model02(); model01.setName("jack66"); model01.setAge(12); model01.setCity("gz"); model02.setName("henry"); model02.setAge("32"); model02.setCity("sz"); String[] ignoreProperties = {"name"}; BeanUtils.copyProperties(model01,model02,ignoreProperties); System.out.println(model01); System.out.println(model02); }}打印结果
Model01{name='jack66', age=12, city='gz'}Model02{name='henry', age='32', city='gz'}
阅读全文
1 0
- BeanUtils中copyProperties的使用
- BeanUtils.copyProperties()的使用
- BeanUtils中copyProperties()方法的使用
- 关于BeanUtils.copyProperties的使用
- 关于BeanUtils.copyProperties的使用
- 关于BeanUtils.copyProperties的使用
- 关于BeanUtils.copyProperties的使用
- 关于BeanUtils.copyProperties的使用
- 使用BeanUtils.copyProperties()产生的错误
- 关于BeanUtils.copyProperties( )方法的使用
- BeanUtils.copyProperties使用
- BeanUtils.copyProperties()使用
- BeanUtils.copyProperties使用
- BeanUtils.copyProperties()的用法
- BeanUtils.copyProperties()的用法
- BeanUtils.copyProperties()的用法
- BeanUtils.copyProperties()的用法
- BeanUtils.copyProperties()的用法
- rpm常用命令及rpm参数介绍
- win10下载ios及安装方法
- zlog
- 自定义CILog日志类的使用(php的CI框架的日志类)
- 算法 (排序乐园Lv-3)
- BeanUtils中copyProperties的使用
- Spring Cloud Config—服务器
- android系统安全策略te语法分析
- tomcat_install
- java 接收解析微信公众号返回XML数据包,消息,地理位置
- SVN-无法查看log,提示Want to go offline,时间显示1970问题
- Javascript之window对象
- flume拦截器使用
- zTree开发使用说明