728x90

2023/08/29 3

Spring - 게시글 조회, 클래스 분리 (단건)

- 게시글 단건 조회 - PostService.java ... public Post get(Long postId) { Post post = postRepository.findById(postId) .orElseThrow(() -> new IllegalArgumentException("존재하지 않는 글입니다.")); return post; } } - Jpa를 통해 postRepository에서 findById를 통해 Id값에 해당하는 Post 엔티티 값을 가져온다. - 기본적으로 값이 없을 경우 null이 반환되기 때문에 Optional로 반환 타입을 지정해야 하지만 참조형을 Post 엔티티로 지정하는 대신 null과 같이 예외가 발생하는 경우 던져버리는 형식으로 바꿔서 사용할 수 있다. * Optiona..

Programming/Spring 2023.08.29

Spring - 클래스 분리, 데이터 저장 (Builder, ObjectMapper, 상황 별Controller 반환 값 등...)

- 데이터 DB에 저장 - 일반적으로 Controller -> (DTO) -> Service -> (DTO) -> Repository -> (Entity, Domain) -> DB 와 같이 클라이언트의 요청이 들어오고 DB에 데이터가 저장되는 흐름이다. - Post.java @Entity @Getter @NoArgsConstructor(access = AccessLevel.PUBLIC) public class Post { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String title; @Lob private String content; @Builder public Post(String title, S..

Programming/Spring 2023.08.29
728x90