보라코딩
카테고리
검색하기
new 보라
« 2025/11 »
일
월
화
수
목
금
토
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
닫기
관리 메뉴
보라코딩
Spring Boot JPA CRUD 정리 본문
코딩/Spring
Spring Boot JPA CRUD 정리
new 보라
2023. 8. 8. 12:12
BlogEntity
package com.boralog.blog.domain; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @Entity(name="boralog") // 서버 실행시 MySQL에 테이블 생성됨 @Data @AllArgsConstructor @NoArgsConstructor @Builder public class BlogEntity { @Id // PK @GeneratedValue(strategy = GenerationType.IDENTITY) //DB 번호증가 전략 private Long id; @Column private String writer; @Column private String content; public BlogEntity(String writer, String content) { this.writer = writer; this.content = content; } }
MainRepository
package com.boralog.blog.domain; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; // JpaRepository를 extends하면 @Repository 생략 가능! // JpaRepository는 CURD 함수 갖고 있음 //@Repository public interface MainRepository extends JpaRepository<BlogEntity, Long> { }
MainService
package com.boralog.blog.service; import java.util.List; import javax.transaction.Transactional; import org.springframework.stereotype.Service; import com.boralog.blog.domain.BlogEntity; import com.boralog.blog.domain.MainRepository; import lombok.RequiredArgsConstructor; // 기능 정의 및 트랜잭션 관리 가능 @Service @RequiredArgsConstructor public class MainService { private final MainRepository mainRepository; //저장하기 @Transactional public BlogEntity saveBlog(BlogEntity blogEntity) { return mainRepository.save(blogEntity); } //모두 조회 @Transactional public List<BlogEntity> all(){ return mainRepository.findAll(); } //하나만 조회 @Transactional public BlogEntity findById(Long id) { return mainRepository.findById(id) .orElseThrow(() -> new IllegalArgumentException("id 확인해주세요")); } //수정하기 @Transactional public BlogEntity updateBlog(Long id, BlogEntity blogEntity) { BlogEntity blog = mainRepository.findById(id) .orElseThrow(() -> new IllegalArgumentException("id 확인해주세요")); blog.setWriter("수정하자아아 글쓴이"); blog.setContent("수정하자아아 내용"); return blog; } //삭제하기 @Transactional public String deleteBlog(Long id) { mainRepository.deleteById(id); return "delete ok"; } }
MainController
package com.boralog.blog.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import com.boralog.blog.domain.BlogEntity; import com.boralog.blog.domain.MainRepository; import com.boralog.blog.service.MainService; @RestController @CrossOrigin("*") public class MainController { @Autowired private MainService mainService; //연습용 @GetMapping("/bora") public ResponseEntity<?> example(){ return new ResponseEntity<String>("ok", HttpStatus.OK); } //모두 찾기 @GetMapping("/main") public ResponseEntity<?> findAll(){ return new ResponseEntity<> (mainService.all(),HttpStatus.OK); } //저장하기 @PostMapping("/main") public ResponseEntity<?> save(BlogEntity blogEntity){ return new ResponseEntity<>(mainService.saveBlog(blogEntity),HttpStatus.CREATED); } //수정하기 @PutMapping("/book/{id}") public ResponseEntity<?> update(@PathVariable Long id, @RequestBody BlogEntity blogEntity){ return new ResponseEntity<>(mainService.updateBlog(id, blogEntity),HttpStatus.OK); } //삭제하기 @DeleteMapping("/book/{id}") public ResponseEntity<?> delete(@PathVariable Long id){ return new ResponseEntity<>(mainService.deleteBlog(id),HttpStatus.OK); } }