0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

H2データベース接続のためのapplication.propertiesの設定例を見る

Posted at

Spring Bootにおける設定ファイル1であるapplication.propertiesについて、H2データベースに接続する際の設定2を例として見ていく3
ここでは、1行ずつ設定を追記していく形式をとる。

1. データベースのJDBC URLを記述する。

application.properties
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE

jdbc:h2:mem:<データベース名>により、インメモリデータベース4としている。接続が切れた際にデータを保持するためDB_CLOSE_DALAY=-1を、終了時にデータベースを閉じることを無効にするためDB_CLOSE_EXIT=FALSEを追加する。

2. JDBCドライバのクラス名を記述する。

application.properties
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driver-class-name=org.h2.Driver

H2データベースにおけるJDBCドライバのクラス名はorg.h2.Driver。PostgreSQLであればorg.postgresql.Driver

3. データベースへのログインユーザ名、ログインパスワードを記述する。

application.properties
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driver-class-name = org.h2.Driver
spring.datasouece.username=sa
spring.datasource.password=

4. SQL文の文字エンコーディングを指定する。

application.properties
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driver-class-name = org.h2.Driver
spring.datasouece.username=sa
spring.datasource.password=
spring.datasource.sql-script-encoding=UTF-8

5. コンソールの有効設定を記述する。

application.properties
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driver-class-name = org.h2.Driver
spring.datasouece.username=sa
spring.datasource.password=
spring.datasource.sql-script-encoding=UTF-8
spring.h2.console.enabled=true

H2コンソールを有効にしたい場合、trueを設定する。

6. データベースの初期化を行うかの設定を記述する。

application.properties
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driver-class-name = org.h2.Driver
spring.datasouece.username=sa
spring.datasource.password=
spring.datasource.sql-script-encoding=UTF-8
spring.h2.console.enabled=true
spring.datasource.initialization-mode=always

alwaysで初期化を行う。その他、embedded, neverが指定可能。以前のSpring Bootのバージョンでは、spring.datasource.initialize=trueであった。

7. DDLおよびDMLのリソース参照を設定する。

application.properties
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driver-class-name = org.h2.Driver
spring.datasouece.username=sa
spring.datasource.password=
spring.datasource.sql-script-encoding=UTF-8
spring.h2.console.enabled=true
spring.datasource.initialization-mode=always
spring.datasource.schema=classpath:schema.sql
spring.datasource.data=classpath:data.sql

指定ファイル前にclasspath:を付ける。schema.sqlにDDLを、data.sqlにDMLを定義する5

  1. アプリケーションにハードコーディングしたくない値を記述するファイル。プロパティファイルとも呼ばれる(拡張子.propertiesがついていている)。

  2. データベースへの接続情報は環境に依存するため、設定ファイルに記述する候補となる。

  3. 中身をよく見ずなんとなく書いて進めようとしている自分に気づいたため。

  4. データおよびその変更を永続化する必要がない場合に用いられる。

  5. DDL(Data Definition Language)は定義する方なので、CREATE/DROP/ALTERなど。DML(Data Manipulation Language)は操作する方なので、SELECT/INSERT/UPDATE/DELETEなど。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?