VS Code + Spring Boot + Maven + JPA + H2 で最速 Web API 環境の構築(Step2)
H2 databases の導入
このステップでは、H2 database をプロジェクトに追加します。
前提条件
依存関係の追加
1.pom.xml を開き、右クリックから「Edit startars」を選択する(もちろん、コマンドパレットから「Edit startars」でも OK) 2. 依存関係に「H2 database」を追加
接続情報および、Debug tool
H2 database の接続情報および、Debug tool の設定を行います。application.properties ファイルに下記の設定を加えます。
# datasource spring.datasource.driver-class-name=org.h2.Driver spring.datasource.url=jdbc:h2:./h2db/sandbox spring.datasource.username=dev spring.datasource.password=dev # h2 for debug tool spring.h2.console.enabled=true spring.h2.console.path=/h2-console spring.h2.console.settings.web-allow-others=true
動作確認
H2 database の起動を確認します。F5(デバッグの開始)で、Tomcat を起動し、下記のURLにアクセスしてください。
H2 Console: http://localhost:8080/h2-console/
Login ダイアログが表示されるので、ログイン情報を入力後、「Connect」押下で、H2 コンソールにログインできれば動作確認完了です。
.gitignore
今回作成した DB 自体は、バージョン管理の必要が無いので、git 管理から除外しておきます。
### H2 database ### /h2db/
ここまでのコードを公開しておきます。良かったら利用してください。