「Java」BeanUtilsクラスのcopyPropertiesメソッドでクラスBeanのフィールドをコピーする
書式
copyProperties(Object dest, Object orig)
戻り値:void
コピー元の各値をコピー先へ設定します。
同じ名前のフィールドの中身をコピーします。
1.コピー元クラス(UserIntro.java)の定義
package com.arkgame.study;
public class UserIntro {
private Integer empId = null;
private String userName = null;
private String addr;
public UserIntro(Integer empId, String userName, String empAddr) {
super();
this.empId = empId;
this.userName = userName;
this.addr = empAddr;
}
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getuserName() {
return userName;
}
public void setuserName(String userName) {
this.userName = userName;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
}
package com.arkgame.study;
public class UserIntro {
private Integer empId = null;
private String userName = null;
private String addr;
public UserIntro(Integer empId, String userName, String empAddr) {
super();
this.empId = empId;
this.userName = userName;
this.addr = empAddr;
}
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getuserName() {
return userName;
}
public void setuserName(String userName) {
this.userName = userName;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
}
package com.arkgame.study; public class UserIntro { private Integer empId = null; private String userName = null; private String addr; public UserIntro(Integer empId, String userName, String empAddr) { super(); this.empId = empId; this.userName = userName; this.addr = empAddr; } public Integer getEmpId() { return empId; } public void setEmpId(Integer empId) { this.empId = empId; } public String getuserName() { return userName; } public void setuserName(String userName) { this.userName = userName; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } }
2.コピー先クラス(UserCopyProp.java)の定義
package com.arkgame.study;
public class UserCopyProp {
private Integer age = null;
private String userName = null;
private String addr = null;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getuserName() {
return userName;
}
public void setuserName(String userName) {
this.userName = userName;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
}
package com.arkgame.study;
public class UserCopyProp {
private Integer age = null;
private String userName = null;
private String addr = null;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getuserName() {
return userName;
}
public void setuserName(String userName) {
this.userName = userName;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
}
package com.arkgame.study; public class UserCopyProp { private Integer age = null; private String userName = null; private String addr = null; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getuserName() { return userName; } public void setuserName(String userName) { this.userName = userName; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } }
3.copyProperties(Object dest, Object orig)
コピー元の属性値をコピー先へセットします。
使用例
package com.arkgame.study;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.BeanUtils;
public class CopyPropertiesDemo {
public static void main(String[] args) {
// UserIntroオブジェクト生成 コンストラクターあり (source側)
UserIntro userIntro = new UserIntro(1001, "User008", "Real Address 007");
// Userオブジェクト生成 コンストラクターなし (dest側)
UserCopyProp userCopyProp = new UserCopyProp();
try {
// コピー元(userIntro) コピー先(user)
BeanUtils.copyProperties(userCopyProp, userIntro);
System.out.println("age属性をコピーしない結果: " + userCopyProp.getAge());
System.out.println("username属性をコピーする結果: " + userCopyProp.getuserName());
System.out.println("addr属性をコピーする結果: " + userCopyProp.getAddr());
} catch (IllegalAccessException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
}
package com.arkgame.study;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.BeanUtils;
public class CopyPropertiesDemo {
public static void main(String[] args) {
// UserIntroオブジェクト生成 コンストラクターあり (source側)
UserIntro userIntro = new UserIntro(1001, "User008", "Real Address 007");
// Userオブジェクト生成 コンストラクターなし (dest側)
UserCopyProp userCopyProp = new UserCopyProp();
try {
// コピー元(userIntro) コピー先(user)
BeanUtils.copyProperties(userCopyProp, userIntro);
System.out.println("age属性をコピーしない結果: " + userCopyProp.getAge());
System.out.println("username属性をコピーする結果: " + userCopyProp.getuserName());
System.out.println("addr属性をコピーする結果: " + userCopyProp.getAddr());
} catch (IllegalAccessException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
}
package com.arkgame.study; import java.lang.reflect.InvocationTargetException; import org.apache.commons.beanutils.BeanUtils; public class CopyPropertiesDemo { public static void main(String[] args) { // UserIntroオブジェクト生成 コンストラクターあり (source側) UserIntro userIntro = new UserIntro(1001, "User008", "Real Address 007"); // Userオブジェクト生成 コンストラクターなし (dest側) UserCopyProp userCopyProp = new UserCopyProp(); try { // コピー元(userIntro) コピー先(user) BeanUtils.copyProperties(userCopyProp, userIntro); System.out.println("age属性をコピーしない結果: " + userCopyProp.getAge()); System.out.println("username属性をコピーする結果: " + userCopyProp.getuserName()); System.out.println("addr属性をコピーする結果: " + userCopyProp.getAddr()); } catch (IllegalAccessException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } catch (InvocationTargetException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } } }
実行結果
age属性をコピーしない結果: null
username属性をコピーする結果: User008
addr属性をコピーする結果: Real Address 007
age属性をコピーしない結果: null
username属性をコピーする結果: User008
addr属性をコピーする結果: Real Address 007
age属性をコピーしない結果: null username属性をコピーする結果: User008 addr属性をコピーする結果: Real Address 007