JPA를 활용해 CRUD 기능을 만들어보자
4. 글 수정
BoardJpaController
@PutMapping(value = "/{id}")
public ApiResponse<BoardDTO> putBoard(@PathVariable int id,
@RequestBody BoardDTO boardDTO){
Board data = boardJpaService.putBoard(id, boardDTO);
return new ApiResponse(true, data);
}
Board
package kr.ac.daegu.springbootapi.boardjpa.model;
import lombok.*;
import javax.persistence.*;
import java.sql.Date;
import java.sql.Time;
import java.time.LocalDate;
import java.time.LocalTime;
@Entity
@Getter
@Setter
@Table(name = "board")
@ToString
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
public class Board {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "author")
private String author;
@Column(name = "subject")
private String subject;
@Column(name = "content")
private String content;
@Column(name = "writeDate")
private LocalDate writeDate;
@Column(name = "writeTime")
private LocalTime writeTime;
@Column(name = "readCount")
private Integer readCount;
@Column(name = "commentCount")
private Integer commentCount;
@Column(name = "password")
private String password;
@Column(name = "replyRootId")
private Integer replyRootId;
@Column(name = "depth")
private Integer depth;
@Column(name = "orderNum")
private Integer orderNum;
@Column(name = "isDel")
private String isDel;
public Board(Integer id,
String author,
String subject,
String content,
LocalDate writeDate,
LocalTime writeTime,
Integer readCount,
Integer commentCount,
String password,
Integer replyRootId,
Integer depth,
Integer orderNum,
String isDel) {
this.id = id;
this.author = author;
this.subject = subject;
this.content = content;
this.writeDate = writeDate;
this.writeTime = writeTime;
this.readCount = readCount;
this.commentCount = commentCount;
this.password = password;
this.replyRootId = replyRootId;
this.depth = depth;
this.orderNum = orderNum;
this.isDel = isDel;
}
}
- @setter 추가
BoardRepository
Optional<Board> findBoardById(int id);
- @Optional
> Java8에서는 Optional<T> 클래스를 사용해 NPE를 방지할 수 있도록 도와준다. Optional<T>는 null이 올 수 있는 값을 감싸는 Wrapper 클래스로, 참조하더라도 NPE가 발생하지 않도록 도와준다. Optional 클래스는 아래와 같은 value에 값을 저장하기 때문에 null이더라도 바로 NPE가 발생하지 않으며, 클래스이기 때문에 각종 메소드를 제공해준다.
> NPE(NullPountException)
- 개발을 할 때 가장 많이 발생하는 예외 중 하나
출처: https://mangkyu.tistory.com/70 [MangKyu's Diary]
BoardJpaService
public Board putBoard(int id, BoardDTO boardDTO) {
Optional<Board> boardData = boardRepository.findBoardById(id);
// 람다식을 사용하여
boardData.ifPresent(selectedBoard -> {
selectedBoard.setAuthor(boardDTO.getAuthor());
selectedBoard.setSubject(boardDTO.getSubject());
selectedBoard.setContent(boardDTO.getContent());
selectedBoard.setPassword(boardDTO.getPassword());
boardRepository.save(selectedBoard);
});
return boardData.orElseGet(boardData::get);
}
- 람다식개념 참고사이트
https://mangkyu.tistory.com/113
'Springboot' 카테고리의 다른 글
2021-10-26 (Springboot - JPA - CRUD(6) ) (0) | 2021.10.26 |
---|---|
2021-10-26 (Springboot - JPA - CRUD(5) ) (0) | 2021.10.26 |
2021-10-25 (Springboot - JPA - CRUD(3) ) (0) | 2021.10.26 |
2021-10-25 (Springboot - JPA - CRUD(2) ) (0) | 2021.10.25 |
2021-10-25 (Springboot - JPA - CRUD(1) ) (0) | 2021.10.25 |