원본 객체를 복사할 때 setter메소드로 복사를 하는데 개수가 많아지면 코드도 길어지고 번거로워진다.
그래서 BeanUtils 클래스의 copyProperties 메소드를 이용하려고 한다.
copyProperties 메소드
: 원본 객체를 복사할 때 사용하는 메소드
사용방법
BeanUtils.copyProperties(source, target);
: source(원본), target(복사대상)
import org.springframework.beans.BeanUtils;
public class copyPropertiesTest {
static class A{
private int num;
private String id;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
static class B{
private int num;
private String id;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
public static void main(String args[]){
A a=new A();
a.setNum(1);
a.setId("A1");
B b=new B();
b.setNum(2);
b.setId("B2");
System.out.println("===========================");
System.out.println("b.getNum() : " + b.getNum());
System.out.println("b.getId() : " + b.getId());
BeanUtils.copyProperties(a,b);
System.out.println("b.getNum() : " + b.getNum());
System.out.println("b.getId() : " + b.getId());
System.out.println("===========================");
}
}
필드가 많지 않을 경우에는 b.setId(a.getId()); 로 필드를 하나씩 설정해서 데이터를 가져올 수 있지만 필드가 많은 경우에는 BeanUtils.copyProperties(a,b); 를 사용해서 한 번에 복사할 수 있다.
[출력]
+ 특정 필드의 복사를 제외하고 싶은 경우
사용방법
BeanUtils.copyProperties(source, target, ignoreProperties);
: ignoreProperties(복사에서 제외할 필드)
package com.example.pharmacyTest;
import org.springframework.beans.BeanUtils;
public class copyPropertiesTest {
static class A{
private int num;
private String id;
private String name;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
static class B{
private int num;
private String id;
private String name;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static void main(String args[]){
A a=new A();
a.setNum(1);
a.setId("A1");
a.setName("AA1");
B b=new B();
b.setNum(2);
b.setId("B2");
b.setName("BB1");
System.out.println("===========================");
System.out.println("b.getNum() : " + b.getNum());
System.out.println("b.getId() : " + b.getId());
System.out.println("b.getName() : " + b.getName());
BeanUtils.copyProperties(a,b,"id","name");
System.out.println("===========================");
System.out.println("b.getNum() : " + b.getNum());
System.out.println("b.getId() : " + b.getId());
System.out.println("b.getName() : " + b.getName());
System.out.println("===========================");
}
}
id와 name을 제외하고 필드를 복사하고 싶은 경우 ignoreProperties에 "id","name"을 넣어주면 된다.
[결과]
자세한 설명은 아래에 링크에서 확인하면 된다.
BeanUtils (Spring Framework 5.3.7 API)
static boolean isSimpleValueType(Class type) Check if the given type represents a "simple" value type: a primitive or primitive wrapper, an enum, a String or other CharSequence, a Number, a Date, a Temporal, a URI, a URL, a Locale, or a Class.
docs.spring.io
'프로그래밍&IT > spring&spring boot' 카테고리의 다른 글
[Spirng 스프링] RestTemplate 대용량 파일 전송 시 OOM(Out Of Memory) 오류 해결 (0) | 2022.08.26 |
---|---|
[Spirng 스프링] 웹사이트 파비콘 적용하기 (0) | 2021.06.26 |
[spring boot 스프링부트] IntelliJ Gradle로 프로젝트 생성 후 mariaDB + Mybatis 연동하기 (0) | 2021.06.03 |