Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.jpapractice.controller;

import com.example.jpapractice.dto.StudentDto;
import com.example.jpapractice.entity.Student;
import com.example.jpapractice.service.StudentService;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -30,7 +31,7 @@ public ResponseEntity<Student> createStudent(@RequestBody Student student) {
* @return 학생 목록
*/
@GetMapping
public ResponseEntity<List<Student>> getAllStudents() {
public ResponseEntity<List<StudentDto>> getAllStudents() {
return ResponseEntity.ok(studentService.getAllStudents());
}

Expand All @@ -40,7 +41,7 @@ public ResponseEntity<List<Student>> getAllStudents() {
* @return 학생 정보
*/
@GetMapping("/{id}")
public ResponseEntity<Student> getStudentById(@PathVariable Long id) {
public ResponseEntity<StudentDto> getStudentById(@PathVariable Long id) {
return ResponseEntity.ok(studentService.getStudentById(id));
}

Expand All @@ -51,7 +52,7 @@ public ResponseEntity<Student> getStudentById(@PathVariable Long id) {
* @return 수정된 학생 정보
*/
@PutMapping("/{id}")
public ResponseEntity<Student> updateStudent(
public ResponseEntity<StudentDto> updateStudent(
@PathVariable Long id,
@RequestBody Student updatedStudent) {
return ResponseEntity.ok(studentService.updateStudent(id, updatedStudent));
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/example/jpapractice/dto/StudentDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,41 @@
import lombok.Getter;
import lombok.Setter;


/**
* DTO(Data Transfer Object)로 해결
* Entity파일들은 DB에 들어가는 데이터 선언 기능만
* 유저에게 받아온 json형식의 요청이나 객체를 반환 해주는 역할은 DTO로 분리
* Entity와 Dto를 활용하여 책임 분리가 명확함
*
* 사용방법
* 하나의 값이 아닌 객체를 반환해줄 때 사용하는 방법으로 ResponseDto RequestDto등으로 구분할 수 도 있음
* 사용할 때는 @Getter,@Setter,@AllArgsConstructor,@NoArgsConstructor를 사용
*
* 왜 해결이 되나?
* 원래 문제가 생겼던 이유는 Entity를 반환하기 때문에 서로 FK관계에 대하여 명시하였기 때문에 JACKSON에서 연속적으로 탐색하기 때문
* Dto로 분리를 해놓으면, 단순하게 가서 정보를 가져오는 것이기 때문에 필드에 있는 것만 직렬화하면 되니까 문제가 없음
*/

/**
* 지금은 순환참조 해결을 위한 dto 사용이기 때문에 간단하게 dto를 선언하여 entity를 사용하지 않고 해결
*
* From/ToEntity를 선언하여 repository에서는 엔티티를
* service에서는 dto를 반환하게 정해놓을 수 있음
* 현재는 service 단에서 convertToDto가 그 기능을 해주고 있음
*
* 기능별로 객체안의 내용이 다를 수 있다면 dto별로 선언해 관리하는 것도 좋은 방법
* request, response, create, update 등 여러가지 형태로 만들어 둘 수도 있음
*/

@Getter
@Setter
public class StudentDto {
private Long id;
private String name;
private Integer age;
// 엔티티에서는 해당 내용을 ClassRoom 객체로 넣어놨었음
// 문자형태로 정보를 남겨놔야 순환참조가 해결됨
private Long classRoomId;
private String classRoomName;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.jpapractice.service;

import com.example.jpapractice.dto.StudentDto;
import com.example.jpapractice.entity.ClassRoom;
import com.example.jpapractice.entity.Student;
import com.example.jpapractice.repository.StudentRepository;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -55,11 +56,14 @@ public StudentDto getStudentById(Long id) {
* @return 수정된 학생 정보
*/
@Transactional
public Student updateStudent(Long id, Student updatedStudent) {
Student existingStudent = getStudentById(id);
public StudentDto updateStudent(Long id, Student updatedStudent) {
StudentDto existingStudent = getStudentById(id);
existingStudent.setName(updatedStudent.getName());
existingStudent.setAge(updatedStudent.getAge());
existingStudent.setClassRoom(updatedStudent.getClassRoom());
if(updatedStudent.getClassRoom() != null) {
ClassRoom classRoom = updatedStudent.getClassRoom();
existingStudent.setClassRoomName(classRoom.getName());
existingStudent.setClassRoomId(classRoom.getId());}
return existingStudent;
}

Expand Down