Python 官方文档:入门教程 => 点击学习
目录BeanUtils.copyProperties()参数的赋值顺序BeanUtils.copyProperties初体验,及其参数含义解释用处案例: 创建一个源类:source创
BeanUtils.copyProperties(x,y)有两个不同的jar包,引入不同的包,赋值的顺序不一样。
分别是:
org.springframework.beans.BeanUtils
org.apache.commons.beanutils.BeanUtils
引入 1 的话,是将 x 赋值给 y
引入 2 的话,是将 y 赋值给 x
BeanUtils.copyProperties是org.springframework.beans.BeanUtils;包下得一个用于对象间得属性赋值,避免了常规操作得一个个get再set属性值,非常好用
private String username;
private String passWord;
private String email;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "book{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
'}';
}
}
private String username;
private String password;
private String email;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "book{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
'}';
}
}
import com.sdqj.system_integration.domain.book;
import com.sdqj.system_integration.domain.book2;
import org.springframework.beans.BeanUtils;
public class test {
public static void main(String[] args) {
book bo=new book();
bo.setEmail("1111");
bo.setPassword("222");
bo.setUsername("333");
book2 bo2=new book2();
BeanUtils.copyProperties(bo,bo2);
System.out.println(bo.toString());
System.out.println(bo2.toString());
}
}
结果展示:
ignoreProperties参数意思是忽略某个参数,使其不被复制,可写多个被忽略得参数
import com.sdqj.system_integration.domain.book;
import com.sdqj.system_integration.domain.book2;
import org.springframework.beans.BeanUtils;
public class test {
public static void main(String[] args) {
book bo=new book();
bo.setEmail("1111");
bo.setPassword("222");
bo.setUsername("333");
book2 bo2=new book2();
BeanUtils.copyProperties(bo,bo2,"username","password");
System.out.println(bo.toString());
System.out.println(bo2.toString());
}
结果展示:
还有源代码中得Class<?> editable参数,不太了解其中得含义,但只能放target(目标类)
public static void main(String[] args) {
book bo=new book();
bo.setEmail("1111");
bo.setPassword("222");
bo.setUsername("333");
book2 bo2=new book2();
BeanUtils.copyProperties(bo,bo2,book2.class);
System.out.println(bo.toString());
System.out.println(bo2.toString());
}
结果如下:
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: BeanUtils.copyProperties()参数的赋值顺序说明
本文链接: https://lsjlt.com/news/134179.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0