春だから Spring やろうぜ(その 6)
いよいよデータベースにアクセス
今回は H2 データベースから全件取得して表示するところまで。
Entity クラスの作成
基本的に JPA なのでそんなに難しくない。Lombok を使っているので @Data
アノテーションを使うと楽。
package jp.mydns.akanekodou.demo; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import lombok.Data; import lombok.NoArgsConstructor; @Entity @Data @NoArgsConstructor public class MyDataEntity { @Id @GeneratedValue private Integer id; private String name; private String mail; public MyDataEntity(String name, String mail) { this.name = name; this.mail = mail; } }
リポジトリの作成
Spring Boot ならではの機能。データベースアクセス用のリポジトリを作成する。
package jp.mydns.akanekodou.demo; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface MyDataEntityRepository extends JpaRepository<MyDataEntity, Integer> { }
何と空っぽのインターフェースである。
コントローラーの修正。
package jp.mydns.akanekodou.demo; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class HelloController { @Autowired MyDataEntityRepository repository; @RequestMapping(value="/", method=RequestMethod.GET) public String index(Model model) { model.addAttribute("title", "Hello Page"); model.addAttribute("message", "MyDataEntity List."); List<MyDataEntity> data = repository.findAll(); model.addAttribute("data", data); return "hello"; } }
@Autowired
が Spring らしい。Spring の根幹であった DI によるインスタンス作成をここで行っている。findAll
なんて実装してないぞ、と思われるだろうが、MyDataEntityRepository
の継承元である JpaRepository
から継承されたメソッドである。
テンプレートの修正。
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title th:text="${title}"/> <link th:href="@{/css/hello.css}" rel="stylesheet" /> </head> <body> <h1 th:text="${title}"/> <p th:text="${message}"/> <table> <tr> <th>ID</th> <th>NAME</th> <th>MAIL</th> </tr> <tr th:each="entity : ${data}"> <td th:text="${entity.id}"/> <td th:text="${entity.name}"/> <td th:text="${entity.mail}"/> </tr> </table> </body> </html>
th:each
による繰り返し表示。
body { padding: 10px; color: #666; } th { padding: 10px; background: #666; color: white; } td { padding: 10px; background: #ddd; color: #333; }
最後に初期データを投入するための細工をする。
package jp.mydns.akanekodou.demo; import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringDemoApplication { @Autowired MyDataEntityRepository repository; public static void main(String[] args) { SpringApplication.run(SpringDemoApplication.class, args); } @PostConstruct public void initialData() { repository.save(new MyDataEntity("tuyano", "syoda@tuyano")); repository.save(new MyDataEntity("hanako", "hanako@flower")); repository.save(new MyDataEntity("sachiko", "sachico@happy")); repository.save(new MyDataEntity("taro", "taro@yamada")); repository.flush(); } }
出た…けど、いまいちデータベースにアクセスした感じがしない!
というわけで次回は PostgreSQL を使ってみることにします。(続く)