일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- @Builder @NoArgsConstructor
- spring repository
- JPA DB 다중 Connection
- 도커 로그 확인
- JPA DB Connection 다중
- Transactions Propagation Option
- JAVA Exception 종류
- JpaRepository update
- Data Annotation
- vueslot
- AllArgsConstructor
- 도커 컨테이너 로그
- NoArgsConstructor
- repository annotation
- 자바버그수정
- docker 로그
- Spring JPA Specification
- mapper annotationo
- CrudRepository update
- Spring
- spring DB Connection
- spring mapper annotation
- vuecomponent
- Vue
- spring JPA DB multi Connection
- spring @mapper
- spring JPA DB Connection
- spring mapper
- 도커 logs
- doker logs tail
- Today
- Total
개발을 잘하고 싶은 개발자
[Spring] DTO, VO, ENTITY 차이를 알고 써보자 본문
MVC의 'M' Model을 설명해볼까?
음... 모델 그 객체 담는....
어디에 담아? DTO, VO, ENTITY가 떠오를 것이다
과연 언제 DTO를 쓰고, VO를 사용하고, 무엇을 ENTITY라고 할까?
https://www.youtube.com/watch?v=J_Dr6R0Ov8E
배민 우아한 TECH에서 발표한 영상도 있다
우리가 생각 없이 /vo 나 /dto 에 몽땅 넣어놓고, MemberDTO, MemberVo라고 붙여버리는 일이 일상 다반사이다
참고 :https://youngjinmo.github.io/2021/04/dto-vo-entity/
이런 개념을 잡을 때 우선 처음에 잡고 가야 할 엔티티부터 설명해본다

1. Entity

실제 DB의 테이블과 같은 컬럼, 자료형으로 매. 핑. 되는 객체이다.
id를 통해 각각의 Entity를 구분하며 테이블을 소스에서 담고, 꺼낼 때 사용한다.
그러니 테이블 객체를 담을땐 DTO, VO가 아닌 Entity를 사용한다!
import javax.persistence.*;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
privae Long id;
@Column(nullable = false)
private String name;
}
2. DTO (Data Transfer Object)
단지 get과 set만 있는(요즘은 @Getter, @Setter, @Data를 많이 쓰지만..)
이런 형태에 데이터를 담고, 꺼내는 용도로 DTO를 사용한다
예를 들어 ResultDTO, ErrorDTO 등등으로 많이 보았다
class RGBColorDto {
private int red;
private int green;
private int blue;
public RGBColor(int red, int green, int blue) {
this.red = red;
this.green = green;
this.blue = blue;
}
public int getRed() {
return red;
}
public int setRed(int red) {
this.red = red;
}
...
}
3. VO (Value Object)
VO는 값 그 자체를 표현하는 객체이다.
로직을 포함할 수 있으며, 객체의 불변성(객체의 정보가 변경하지 않음)을 보장한다.
VO의 핵심 역할은 equals()와 hashcode() 를 오버라이딩하는 것이다.
즉, VO 내부에 선언된 속성(필드)의 모든 값들이 VO 객체마다 값이 같아야, 똑같은 객체라고 판별한다
class RGBColor {
private final int red;
private final int green;
private final int blue;
public RGBColor(int red, int green, int blue) {
this.red = red;
this.green = green;
this.blue = blue;
}
public static RGBColor of(int red, int green, int blue){
return new RGBColor(red, green, blue);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RGBColor rgbColor = (RGBColor) o;
return red == rgbColor.red && green == rgbColor.green && blue == rgbColor.blue;
}
}
'Backend > spring' 카테고리의 다른 글
[Spring] JPA DB Connection 다중으로 사용하기! (0) | 2021.08.03 |
---|---|
[Spring JPA] JpaRepository update업데이트 하고 싶지만 save (0) | 2021.08.02 |
[Spring] RESTful 서비스 설계 - Exception 설계한 Response 처리 (0) | 2021.07.24 |
[Spring JPA] PROCEDURE 프로시져 호출 (0) | 2021.07.17 |
servelt-mapping error Cannot resolve Servlet... (0) | 2016.01.28 |