0
0

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 5 years have passed since last update.

[Spring Boot] JDBCを利用してDBのテーブルを作る際は初期化が必要

Posted at

環境

  • Java 11.0.2
  • Spring Boot 2.2.0
  • データベース MySQL 8.0.18

はじめに

'schema.sql'にDBのテーブル情報を定義したのですが、レコードの内容を表示する際にエラーが出てしまう。
アプリの起動は出来ているのでDBとの接続が原因ではない。おかしいなぁとDBを確認したらテーブルが作成されていませんでした。

解決方法

YAMLファイルにDB初期化の設定を追加したら無事にテーブルが作成されました。

application.yml
spring:
  datasource:
    initialization-mode: always
    url: jdbc:mysql://localhost:3306/springboot-db?autoReconnect=true&useSSL=false&
      serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver

SQLファイルを使ったDBを初期化する場合は以下の設定を追加する必要があるのですね。
spring.datasource.initialization-mode=always

参考文献

Spring Boot 2.0のカテゴリ別重要プロパティ一覧
Spring Boot Reference

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?