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 1 year has passed since last update.

SpringBoot data.sqlの日本語が文字化けするときの対応

Posted at

前提

  • MySQL 8
  • SpringBoot 2.6.7

課題

data.sqlでデータを入れようとしたら文字化けする。

application.yml
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mysqldb
    username: test
    password: test
    driver-class-name: com.mysql.cj.jdbc.Driver
  sql:
    init:
      mode: always
schema.sql
CREATE TABLE IF NOT EXISTS m_motorcycle (
    moto_no int NOT NULL PRIMARY KEY,
    moto_name varchar(20)
);
data.sql
INSERT INTO m_motorcycle (moto_no, moto_name) VALUES (1, 'ばいくめい1');
INSERT INTO m_motorcycle (moto_no, moto_name) VALUES (1, 'ばいくめい2');

data.sqlはUTF-8

image.png

image.png

解決

sql-init-encoding=UTF-8を設定追記することで解消できた。

application.yml
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mysqldb
    username: test
    password: test
    driver-class-name: com.mysql.cj.jdbc.Driver
  sql:
    init:
      mode: always
      encoding: UTF-8

image.png

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?