🐙

[メモ]Spring 公式のMySQLデータアクセスのチュートリアルにはめられた

2021/05/13に公開

こんにちはyoshimokです。
Spring Boot使ってMySQLにつなげたいなーって思って公式のドキュメント見てたら、怒られたのでメモ書き。

やりたかったこと

Spring Boot + KotlinのアプリケーションでMySQL使いたかっただけ。
MySQLはdockerコンテナで動かしています。

問題のドキュメント

https://spring.pleiades.io/guides/gs/accessing-data-mysql/#initial
こちらです。

試してみた

Gradle.buildに諸々追加。

dependencies {
	implementation("org.springframework.boot:spring-boot-starter-data-jpa")
	implementation("org.springframework.boot:spring-boot-starter-web")
	implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
	implementation("org.jetbrains.kotlin:kotlin-reflect")
	implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
	runtimeOnly("mysql:mysql-connector-java") // 追加したもの
	testImplementation("org.springframework.boot:spring-boot-starter-test")
	implementation(kotlin("script-runtime"))
}

よさそう。

既にh2を使って実装していたのでapplication.propertiesの内容を書き換えます。

これが

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:file:./data/testdb
spring.datasource.username=sa
spring.datasource.password=password
spring.datasource.initialization-mode=always

こんな感じになりました。

spring.datasource.driver-class-name =com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update

動かしてみた

イイ感じっぽいので動かしてみました。

すると...

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

なんか怒られました。

なんで?

エラーでも書いてありますが、
com.mysql.jdbc.Driverは非推奨らしいです。公式の教えに則っただけなのに。。

解決

エラーに書いてある通りcom.mysql.cj.jdbc.Driveに変えたらエラーは消えました。

それだけ

おわりに

薄い内容ですが、勘弁してください。
もしかしたら僕の間違いかもしれないので有識者の方ご指摘ください。

Kotlin Spring Boot実際爆速で構築できていい感じなので、また何かしら投稿しようかと思います。

Discussion