From fab0deb3660e0799a44474c89fb13f4b97798f0f Mon Sep 17 00:00:00 2001 From: Riverstone00 Date: Fri, 25 Jul 2025 16:07:00 +0900 Subject: [PATCH 1/2] =?UTF-8?q?MySQL=EC=97=B0=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 3 ++- src/main/resources/application.properties | 17 ++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/build.gradle b/build.gradle index b2fc2de..92d7ad9 100644 --- a/build.gradle +++ b/build.gradle @@ -21,7 +21,8 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' - runtimeOnly 'com.h2database:h2' +// runtimeOnly 'com.h2database:h2' + runtimeOnly 'com.mysql:mysql-connector-j' testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 117142e..25c2fdf 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,12 +1,15 @@ spring.application.name=BE_Basic_Security_Web_Assignment -spring.datasource.url=jdbc:h2:tcp://localhost/~/test -spring.datasource.driver-class-name=org.h2.Driver -spring.datasource.username=sa -spring.datasource.password= +spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false&serverTimezone=Asia/Seoul +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +spring.datasource.username=root +spring.datasource.password=mysql:data!235711 spring.jpa.hibernate.ddl-auto=update -spring.jpa.database-platform=org.hibernate.dialect.H2Dialect +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.format_sql=true +spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect -spring.h2.console.enabled=true -spring.h2.console.path=/h2-console \ No newline at end of file + +#spring.h2.console.enabled=true +#spring.h2.console.path=/h2-console \ No newline at end of file From aa0477740e195d779e4ee41efdc71bae3de7f224 Mon Sep 17 00:00:00 2001 From: Riverstone00 Date: Thu, 31 Jul 2025 00:08:13 +0900 Subject: [PATCH 2/2] =?UTF-8?q?API=EC=9E=91=EC=84=B1=20=EB=B0=8F=20?= =?UTF-8?q?=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/RestAPIController.java | 56 +++++++++++++++++++ .../service/PostService.java | 1 + src/main/resources/templates/detail.html | 2 +- 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/main/java/BE_Basic_Security_Web_Assignment/BE_Basic_Security_Web_Assignment/controller/RestAPIController.java diff --git a/src/main/java/BE_Basic_Security_Web_Assignment/BE_Basic_Security_Web_Assignment/controller/RestAPIController.java b/src/main/java/BE_Basic_Security_Web_Assignment/BE_Basic_Security_Web_Assignment/controller/RestAPIController.java new file mode 100644 index 0000000..f871517 --- /dev/null +++ b/src/main/java/BE_Basic_Security_Web_Assignment/BE_Basic_Security_Web_Assignment/controller/RestAPIController.java @@ -0,0 +1,56 @@ +package BE_Basic_Security_Web_Assignment.BE_Basic_Security_Web_Assignment.controller; + +import BE_Basic_Security_Web_Assignment.BE_Basic_Security_Web_Assignment.entity.Post; +import BE_Basic_Security_Web_Assignment.BE_Basic_Security_Web_Assignment.service.PostService; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/api") +public class RestAPIController { + private final PostService postService; + + public RestAPIController(PostService postService) { + this.postService = postService; + } + + @GetMapping("/posts") + public ResponseEntity> getAllPosts() { + return ResponseEntity.ok(postService.findAll()); + } + + @GetMapping("/posts/{id}") + public ResponseEntity getPost(@PathVariable Long id) { + Post post = postService.findById(id); + return post != null ? ResponseEntity.ok(post) : ResponseEntity.notFound().build(); + } + + @PostMapping("/posts") + public ResponseEntity createPost(@RequestBody Post post) { + Post savedPost = postService.save(post); + return ResponseEntity.ok(savedPost); + } + + @PutMapping("/posts/{id}") + public ResponseEntity updatePost(@PathVariable Long id, @RequestBody Post updatedPost) { + Post existingPost = postService.findById(id); + if (existingPost == null) { + return ResponseEntity.notFound().build(); + } + updatedPost.setId(id); + Post savedPost = postService.save(updatedPost); + return ResponseEntity.ok(savedPost); + } + + @DeleteMapping("/posts/{id}") + public ResponseEntity deletePost(@PathVariable Long id) { + if (postService.findById(id) == null) { + return ResponseEntity.notFound().build(); + } + postService.deleteById(id); + return ResponseEntity.noContent().build(); + } + +} diff --git a/src/main/java/BE_Basic_Security_Web_Assignment/BE_Basic_Security_Web_Assignment/service/PostService.java b/src/main/java/BE_Basic_Security_Web_Assignment/BE_Basic_Security_Web_Assignment/service/PostService.java index 98d5f33..e0f8d6a 100644 --- a/src/main/java/BE_Basic_Security_Web_Assignment/BE_Basic_Security_Web_Assignment/service/PostService.java +++ b/src/main/java/BE_Basic_Security_Web_Assignment/BE_Basic_Security_Web_Assignment/service/PostService.java @@ -28,5 +28,6 @@ public Post findById(Long id) { public void deleteById(Long id) { Post post = postRepository.findById(id).orElse(null); + postRepository.delete(post); } } diff --git a/src/main/resources/templates/detail.html b/src/main/resources/templates/detail.html index e77e4bd..c90689a 100644 --- a/src/main/resources/templates/detail.html +++ b/src/main/resources/templates/detail.html @@ -7,7 +7,7 @@

제λͺ©

λ‚΄μš©

-πŸ”™ λͺ©λ‘μœΌλ‘œ +πŸ”™ λͺ©λ‘μœΌλ‘œ ✏️ μˆ˜μ • πŸ—‘οΈ μ‚­μ œ