13
15

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.

Spring Boot DB関連の豆知識

Last updated at Posted at 2018-04-08

Spring Boot を『はじめてのSpring-Boot』にて勉強中。
DB関連の設定で、いいこと書いていたので紹介します。

自動読み込みされるSQLファイル

Spring Bootではクラスパス直下(src/main/resources/*.sql)に以下のSQLファイルが存在すれば読み込んで実行される。

schema-(platform).sql
schema.sql
data-(platform).sql
data.sql

SQLファイルに日本語を使う場合

src/main/resources/application.properties

spring.datasource.sql-script-encoding=UTF-8

H2のDB設定

src/main/resources/application.properties

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

永続化したければurlを以下のように変更

spring.datasource.url=jdbc:h2:file:./target/db/testdb

永続化する場合の設定

spring.datasource.url=jdbc:h2:file:./target/db/testdb

Log4JDBCを使ったSQLログ出力

  • pom.xml
<dependency>
	<groupId>org.lazyluke</groupId>
	<artifactId>log4jdbc-remix</artifactId>
	<version>0.2.7</version>
</dependency>
  • Log4JDBC用のJDBCドライバ設定

src/main/resources/application.properties

spring.datasource.driver-class-name=net.sf.log4jdbc.DriverSpy
spring.datasource.url=jdbc:log4jdbc:省略
  • Log4JDBC用ログレベル設定

src/main/resources/application.properties

logging.level.jdbc=OFF
logging.level.jdbc.sqltiming=DEBUG

リファレンス

13
15
2

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
13
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?