Blog

[Spring]20_1 MVC패턴 CRUD 구현 흐름 파악_DTO와 Entity (Controller가 모두 부담하는 최초 형태)

Author
Summary
MVC 패턴 최초 구현 기본형
Category
Study
Tags
Spring
Favorite
Memory Date
2023/08/31
Cross Reference Study
Related Media
Related Thought
Related Lessons
tag
날짜
작성자
진행상황
진행 전
태그구분
6 more properties
DTO란?
DTO(Data Transfer Object)는 데이터 전송 및 이동을 위해 생성되는 객체를 의미
Client에서 보내오는 데이터를 객체로 처리할 때 사용
서버의 계층간의 이동에도 사용
DB와의 소통을 담당하는 Java 클래스를 그대로 Client에 반환하는 것이 아니라 DTO로 한번 변환한 후 반환할 때도 사용
RequestDto, Response를 할 때 사용되는 객체는 ResponseDto라는 이름을 붙여 DTO 클래스 생성
BlogRequestDto.java
package com.yzpocket.springmvcblog.dto; import lombok.Getter; import lombok.Setter; import java.sql.Timestamp; //Memo 클래스와 비슷하게 생겼다. 요청에 대하여 매개변수로 받아내는 부분이다. 컨트롤러 생성 후 이것을 작성한다. @Getter @Setter public class BlogRequestDto { //private int idx; private String title; private String author; private String contents; private Timestamp accessTime; private String password; }
Java
복사
BlogResponseDto.java
package com.yzpocket.springmvcblog.dto; import com.fasterxml.jackson.annotation.JsonFormat; import com.yzpocket.springmvcblog.entity.Blog; import lombok.Getter; import lombok.Setter; import java.sql.Timestamp; //Blog 클래스와 비슷하게 생겼다. 요청에대한 반환 부분이다. 컨트롤러 생성 후 이것을 작성한다. @Getter @Setter public class BlogResponseDto { private Long idx; private String title; private String author; private String contents; @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+9") private Timestamp accesstime; private String password; public BlogResponseDto(Blog blog) { this.idx = blog.getIdx(); this.title = blog.getTitle(); this.author = blog.getAuthor(); this.contents = blog.getContents(); this.accesstime = blog.getAccesstime(); this.password = blog.getPassword(); } public BlogResponseDto(String title, String author, String contents, Timestamp accesstime) { this.title = title; this.author = author; this.contents = contents; this.accesstime = accesstime; } public BlogResponseDto(Long idx, String title, String author, String contents, Timestamp accesstime, String password) { this.idx = idx; this.title = title; this.author = author; this.contents = contents; this.accesstime = accesstime; this.password = password; } }
Java
복사
Entity란?
실질 객체 = Java에서 객체를 위해 생성하던 그 클래스가 맞다.
DTO는 Data를 담고 이동하는 객체라면, Entity는 가공을 위해 DTO가 잠시 서버 내부에서 변환된 상태로 가공 등을 거친다 생각하자. 이후 반환 할 때는 마찬가지로 Data를 담아 이동해야 하기 때문에 DTO로 변환 되어야 한다.
package com.yzpocket.springmvcblog.entity; // 이부분, 예전에 했었던 MemoVO 이거랑 개념이 살짝 다르다 // DTO는 계층간 데이터 전송&이동을 위해 사용되는 객체 (Data Transfer Object) // - 순수 자바 클래스인데 데이터이동을 위해 사용되는 것이구나! // -- 서버 계층간? 계층은뭘까 레이어드 아키텍쳐 // VO는 값을 갖는 순수한 도메인 // Entity는 이를 DB테이블과 매핑하는객체. (DTO로 변환되서 이동해야한다?) // 잘이해가 안된다. 좀 구현하고 찾아보도록하자. // https://youtu.be/J_Dr6R0Ov8E?si=yiyzhHaur2uHfzNF << 테코톡 우아한테크 라흐의 DTO vs VO // DTO 클래스 명칭은 RequestDTO, ResponseDTO 처럼 한다. import com.yzpocket.springmvcblog.dto.BlogRequestDto; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import java.sql.Timestamp; @Getter @Setter @NoArgsConstructor public class Blog { private Long idx; // 글번호 private String title; //글제목 private String author; //작성자 private String contents; //내용 private Timestamp accesstime; //시간 private String password; //비밀번호 public Blog(BlogRequestDto requestDto) { this.title = requestDto.getTitle(); this.author = requestDto.getAuthor(); this.contents = requestDto.getContents(); this.password = requestDto.getPassword(); //this.accesstime = requestDto.getAccesstime(); this.accesstime = new Timestamp(System.currentTimeMillis()); } public void update(BlogRequestDto requestDto) { this.title = requestDto.getTitle(); this.author = requestDto.getAuthor(); this.contents = requestDto.getContents(); this.password = requestDto.getPassword(); this.accesstime = new Timestamp(System.currentTimeMillis()); } }
Java
복사