BeanUtils.copyProperties(source, target)用法
来源:互联网 发布:淘宝云客服多久结工资 编辑:程序博客网 时间:2024/05/29 19:12
BeanUtils.copyProperties(source, target);是spring中的一个方法
顾名思义 copy:复制的意思
Properties:Propertie的复数形式,Propertie属性的意思,复数就是多个属性。
source:源
target:目标
串起来就可以知道这个方法的作用是拷贝源Bean的多个(当然可以一个)属性的值到目标Bean的相同属性中。
例子
1.定义一个Tuser类
public class Tuser implements java.io.Serializable {private String id;private String pwd;private String name;private Integer sex;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getSex() {return sex;}public void setSex(Integer sex) {this.sex = sex;}}
public class User implements java.io.Serializable {private String id;private String pwd;private String name;private Integer age;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}}
如果我们获取了一个tuser对象,需要把tuser中个属性的值赋给user中,那么当相同属性较少的时候可以直接使用get和set的放发,如果相同属性特别多的时候就需要写很多的get和set方法了,那么,此时就可以使用此方法。
例子如下
public class test1 {User user = new User();user.setId(tuser.getId());user.setPwd(tuser.getPwd());user.setName(tuser.getName());}public class test2 {User user = new User();BeanUtils.copyProperties(tuser, user);}
这两种方法是等同的,这样子是不是可以简写很多代码?
0 0
- BeanUtils.copyProperties(source, target)用法
- BeanUtils.copyProperties(source, target)用法
- 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() 用法
- UVa 10420 - List of Conquests
- Java Native Interface
- jstl中fn表达式的应用
- AVPlayer 监听播放时长错误capturing 'self' strongly in this block is likely to lead to a retain cycle 的解决方法
- 虚拟地址与物理地址 -神贴
- BeanUtils.copyProperties(source, target)用法
- java面试题
- aapt的使用
- hdu1008简单电梯时间计算
- bootstrap 3 nav-tab 用法
- UML DESIGNER 教程 好不容易才找到!!!
- 硬件知识【0】:TTL电平和CMOS电平
- [笔记]ubuntu 13.04 配置YouCompleteMe
- A. Counting Sticks