Memo

メモ > 技術 > フレームワーク: SpringBoot > 処理の階層化

■処理の階層化
■処理の階層化の実践 Controller → Service → Repository → Entity という階層化が定番実装方法の一つ。これに従ってプログラムを調整する (以下では、あわせてテンプレートファイルも配置を調整している) src/main/java/com/example/demo/entity/Task.java(src/main/java/com/example/demo/Task.java を移動&編集)
package com.example.demo.entity; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Table; import jakarta.persistence.Id; @Entity @Table(name="tasks") public class Task { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; private String title; private String text; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getText() { return text; } public void setText(String text) { this.text = text; } }
src/main/java/com/example/demo/repository/TaskRepository.java(src/main/java/com/example/demo/TaskRepository.java を移動&編集)
package com.example.demo.repository; import com.example.demo.entity.Task; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface TaskRepository extends JpaRepository<Task, Integer> { }
src/main/java/com/example/demo/service/TaskService.java を新規に作成
package com.example.demo.service; import com.example.demo.entity.Task; import com.example.demo.repository.TaskRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class TaskService { @Autowired private TaskRepository taskRepository; public List<Task> select() { List<Task> tasks = taskRepository.findAll(); return tasks; } public void create() { Task task = new Task(); task.setId(null); task.setTitle("テスト"); task.setText("テスト。\nこれはリポジトリのテストです。"); taskRepository.save(task); } }
src/main/java/com/example/demo/controller/HomeController.java(src/main/java/com/example/demo/HomeController.java を移動&編集)
package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import java.time.LocalDateTime; @Controller public class HomeController { @GetMapping(value = "/") String index() { return "home/index"; } @GetMapping(value = "/now") String now(Model model) { model.addAttribute("time", LocalDateTime.now()); return "home/now"; } }
src/main/java/com/example/demo/controller/TaskController.java を新規に作成
package com.example.demo.controller; import com.example.demo.entity.Task; import com.example.demo.service.TaskService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import java.util.List; @Controller public class TaskController { @Autowired private TaskService taskService; @GetMapping(value = "/task/") String index(Model model) { List<Task> tasks = taskService.select(); model.addAttribute("tasks", tasks); return "task/index"; } @GetMapping(value = "/task/add") String add() { taskService.create(); return "task/add"; } }
src/main/resources/templates/home/index.html(src/main/resources/templates/index.html を移動&編集 / 内容は変わらず)
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Spring Boot</title> </head> <body> <h1>Spring Boot</h1> <p>It works!</p> </body> </html>
src/main/resources/templates/home/now.html(src/main/resources/templates/now.html を移動&編集 / 内容は変わらず)
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Spring Boot</title> </head> <body> <h1>Spring Boot</h1> <p>現在時刻は<span th:text="${time}"></span>です。</p> </body> </html>
src/main/resources/templates/task/index.html(src/main/resources/templates/task.html を移動&編集 / 内容は変わらず)
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Spring Boot</title> </head> <body> <h1>Spring Boot</h1> <p>タスクは以下のとおりです。</p> <table> <tr> <th>ID</th> <th>タイトル</th> <th>テキスト</th> </tr> <tr th:each="task : ${tasks}"> <td th:text="${task.id}"></td> <td th:text="${task.title}"></td> <td th:text="${task.text}"></td> </tr> </table> </body> </html>
src/main/resources/templates/task/add.html(src/main/resources/templates/task_add.html を移動&編集 / 内容は変わらず)
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Spring Boot</title> </head> <body> <h1>Spring Boot</h1> <p>タスクを登録しました。</p> </body> </html>
http://localhost:8080/ http://localhost:8080/now http://localhost:8080/task/ http://localhost:8080/task/add ■参考 以下を参考に、ひととおりの流れを作ってみると良さそう フォーム投稿&バリデーションについても触れられている Spring Boot + Thymeleafで新規登録画面を作成する - ITを分かりやすく解説 https://medium-company.com/spring-boot-thymeleaf%E3%81%A7%E6%96%B0%E8%A6%8F%E7%99%BB%E9%8C%B2%E7%94%... SpringBoot/各レイヤの責務 - KobeSpiral2021 https://cs27.org/wiki/kobespiral2021/?SpringBoot/%E5%90%84%E3%83%AC%E3%82%A4%E3%83%A4%E3%81%AE%E8%B2... Spring Bootで実装するときに気をつけて欲しいポイントベスト3〜構成編〜 - 自主的20%るぅる https://www.agent-grow.com/self20percent/2020/04/13/spring-boot-check-point-structure/ Spring Bootアプリケーションのコードレビューポイント - Qiita https://qiita.com/cross-xross/items/144f8bde2ef6fa4b379f @Component、@Service、@Repository、@Controllerの違いについて - Qiita https://qiita.com/KevinFQ/items/abc7369cb07eb4b9ae29 Spring Boot Thymeleaf でリンク(a href)を記載する方法 | ITエンジニアの定時退社Tips https://www.early2home.com/blog/programming/html/post-1564.html

Advertisement