折腾:
【已解决】spring boot中添加访问操作mysql数据库
期间,从Spring Boot中的JPA生成的Mysql的表结构
-》很明显看出:
Encoding:不是我们希望的utf-8
Collation:也不是我们希望的 utf8mb4 之类的
-》
默认是latin的charset
以及Encoding和Collation都不是我们希望的
所以要去想办法把表结构改为我们希望的
好像加上:
?useUnicode=yes&characterEncoding=UTF-8
就可以了?
但是感觉也缺少Collation啊
jpa charset collation
jpa jdbc charset
jdbc charset
jdbc:ctree:port@host_name:db_name?characterEncoding=encoding
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname?useUnicode=yes&characterEncoding=UTF-8","username", "password");
看了半天感觉是:
此处的jdbc
jdbc:mysql://localhost:3306/dbname?useUnicode=yes&characterEncoding=UTF-8
只是连接时的参数,而不是创建mysql表的时候的参数
所以应该是
src/server/xxx/xxx/src/main/resources/application.properties
spring.jpa.hibernate.ddl-auto=update
中的jpa和hibernate的ddl,去创建的mysql
spring.jpa.hibernate mysql charset
好像是:
mysql的自己的配置my.cnf
中配置
[mysql] default-character-set=utf8 [mysqld] character-set-server=utf8
可以让mysql自动创建表时,charset是utf-8
-》难道是:
hibernate或jpa,生成mysql时,没法指定默认charset?
只能是mysql自己的配置?
spring.datasource.url=jdbc:MySQL://localhost:3306/my-first?characterEncoding=utf8&useSSL=false spring.datasource.connection-init-sql ="SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;"
好像是用connection-init-sql可以实现我们的目的?
另外顺带也注意时区:
show variables like '%time_zone%'; set global time_zone='+8:00';
此处的是:
mysql> show variables like '%time_zone%'; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | system_time_zone | CST | | time_zone | SYSTEM | +------------------+--------+ 2 rows in set (0.02 sec)
暂时不去改。后续(部署到服务器端后)需要再改。
spring.datasource.connection-init-sql
initSQL – the ability to run an SQL statement exactly once, when the connection is created
- spring.datasource.connection-init-sql 指定连接被创建,再被添加到连接池之前执行的sql.
- spring.datasource.connection-init-sqls 使用DBCP connection pool时,指定初始化时要执行的sql
- spring.datasource.init-sql当连接创建时,执行的sql
{ "sourceType": "com.zaxxer.hikari.HikariDataSource", "name": "spring.datasource.hikari.connection-init-sql", "type": "java.lang.String" },
- spring.datasource.dbcp2.connection-init-sqls
- spring.datasource.hikari.connection-init-sql
但是却没找到:
- spring.datasource.connection-init-sql
- spring.datasource.connection-init-sqls
- spring.datasource.init-sql
Spring Boot spring.datasource
如果用,则是:
<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.4.2</version> </dependency>
自己此处项目中,自动生成的
src/server/xxx/xxx/.factorypath
中有:
<factorypathentry kind="VARJAR" id="M2_REPO/com/zaxxer/HikariCP/3.4.1/HikariCP-3.4.1.jar" enabled="true" runInBatchMode="false"/>
对于DataSource来说,选择顺序:
- 优先选:HikariCP
- 对应配置:spring.datasource.hikari.*
- 其次选:Tomcat
- 对应配置:spring.datasource.tomcat.*
- 最后选:Commons DBCP2
- 对应配置:spring.datasource.dbcp2.*
中找到了
spring.datasource.hikari.connection-init-sql
其他的是:
spring.datasource.hikari.allow-pool-suspension spring.datasource.hikari.auto-commit spring.datasource.hikari.catalog spring.datasource.hikari.connection-test-query spring.datasource.hikari.connection-timeout spring.datasource.hikari.data-source-class-name spring.datasource.hikari.data-source-j-n-d-i spring.datasource.hikari.data-source-properties spring.datasource.hikari.driver-class-name spring.datasource.hikari.health-check-properties spring.datasource.hikari.health-check-registry spring.datasource.hikari.idle-timeout spring.datasource.hikari.initialization-fail-timeout spring.datasource.hikari.isolate-internal-queries spring.datasource.hikari.jdbc-url spring.datasource.hikari.leak-detection-threshold spring.datasource.hikari.login-timeout spring.datasource.hikari.max-lifetime spring.datasource.hikari.maximum-pool-size spring.datasource.hikari.metric-registry spring.datasource.hikari.metrics-tracker-factory spring.datasource.hikari.minimum-idle spring.datasource.hikari.password spring.datasource.hikari.pool-name spring.datasource.hikari.read-only spring.datasource.hikari.register-mbeans spring.datasource.hikari.scheduled-executor spring.datasource.hikari.schema spring.datasource.hikari.transaction-isolation spring.datasource.hikari.username spring.datasource.hikari.validation-timeout
再去找找
spring.datasource.init-sql
发现并不存在这个配置
再去搜搜
spring.datasource.connection-init-sql
看来是:
也不存在spring.datasource.connection-init-sql
所以,现在只剩下:
spring.datasource.hikari.connection-init-sql
“🔠connectionInitSql
This property sets a SQL statement that will be executed after every new connection creation before adding it to the pool. If this SQL is not valid or throws an exception, it will be treated as a connection failure and the standard retry logic will be followed. Default: none”
去试试
spring.datasource.hikari.connection-init-sql=SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci
不过要先去删除之前数据库
就空了:
再去新建表
发现不需要创建,已有对应database了。
然后重新运行项目:
注意到:
Control+C中断当前调试时,也显示Hikari的Pool是shutdown关闭:
2020-02-01 19:59:37,228 INFO AbstractEntityManagerFactoryBean.java:598 - Closing JPA EntityManagerFactory for persistence unit 'default' 2020-02-01 19:59:37,232 INFO HikariDataSource.java:350 - HikariPool-5 - Shutdown initiated... 2020-02-01 19:59:37,241 INFO HikariDataSource.java:352 - HikariPool-5 - Shutdown completed. ^C^C[INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------
重新运行,结果:
问题依旧:
改为:
spring.datasource.connection-init-sql ="SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;" spring.datasource.hikari.connection-init-sql="SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci" spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/xx?useUnicode=yes&characterEncoding=UTF-8
结果:
报错:
2020-02-01 20:10:15,650 WARN JdbcEnvironmentInitiator.java:132 - HHH000342: Could not obtain connection to query metadata : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci"' at line 1
试试:
spring.datasource.connection-init-sql =SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' spring.datasource.hikari.connection-init-sql=SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_ci'
结果:
问题依旧:
spring.datasource.hikari.connection-init-sql
还是不行。
搜:
MySQL5InnoDBDialect
spring.datasource.connection-init-sql=SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_ci'; spring.datasource.hikari.connection-init-sql=SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_ci';
问题依旧
spring.datasource.connection-init-sql="SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;"
问题依旧。
jpa MySQL5InnoDBDialect
MySQL5InnoDBDialect
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
SpringBoot整合JPA的踩坑点 | 梦起飞
旧,SpringBoot2.0之前:
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
新,SpringBoot2.0之后:
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
“MySQL5InnoDBDialect
Deprecated
Use “hibernate.dialect.storage_engine=innodb” environment variable or JVM system property instead.”
加上配置
src/server/xxx/xxx/src/main/resources/application.properties
spring.jpa.database-platform=com.crifan.xxx.MySQL5InnoDBDialectUtf8mb4
以及:
src/server/xxx/xxx/src/main/java/com/crifan/xxx/MySQL5InnoDBDialectUtf8mb4.java
import org.hibernate.dialect.MySQL5InnoDBDialect; public class MySQL5InnoDBDialectUtf8mb4 extends MySQL5InnoDBDialect { @Override public String getTableTypeString() { return "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci"; } }
结果却又提示:
The type MySQL5InnoDBDialect is deprecated Java(16777221)
换成:MySQL55Dialect
src/server/xxx/xxx/src/main/java/com/crifan/xxx/MySQL55DialectUtf8mb4.java
package com.crifan.xxx; import org.hibernate.dialect.MySQL55Dialect;; // import org.hibernate.dialect.MySQL5InnoDBDialect; // public class MySQL5InnoDBDialectUtf8mb4 extends MySQL5InnoDBDialect { public class MySQL5InnoDBDialectUtf8mb4 extends MySQL55Dialect { @Override public String getTableTypeString() { return "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci"; } }
结果:
至少编译没报错。
以及:
src/server/xxx/xxx/src/main/resources/application.properties
spring.jpa.database-platform=com.crifan.xxx.MySQL55DialectUtf8mb4
去试试
终于可以了:
【总结】
此处,不需要要去修改mysql的配置,只想要自己此处通过配置指定JPA所创建mysql的table时,指定对应charset是utf8mb4,以及Collation是utf8mb4_unicode_ci
则具体做法是:
创建一个mysql的dialect的类,用于设置table的type的string
src/server/xxx/xxx/src/main/java/com/crifan/xxx/MySQL55DialectUtf8mb4.java
package com.crifan.xxx; import org.hibernate.dialect.MySQL55Dialect;; // import org.hibernate.dialect.MySQL5InnoDBDialect; // public class MySQL5InnoDBDialectUtf8mb4 extends MySQL5InnoDBDialect { public class MySQL55DialectUtf8mb4 extends MySQL55Dialect { @Override public String getTableTypeString() { return "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci"; } }
然后把这个dialect去设置给jpa
src/server/xxx/xxx/src/main/resources/application.properties
spring.jpa.database-platform=com.crifan.xxx.MySQL55DialectUtf8mb4
如此,创建出来的table就是utf8的了。
其中,可以从table info
CREATE TABLE `iec104` ( `id` int(11) NOT NULL, `data` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `parse_result` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
看出来其中有对应的:
ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
注:
(1)另外,创建表是utf8了,连接时也要用utf8,即:
src/server/xxx/xxx/src/main/resources/application.properties
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/xx?useUnicode=yes&characterEncoding=UTF-8
中是加了:
useUnicode=yes&characterEncoding=UTF-8
才可以。
(2)如果不用此配置,也可以换去修改mysql的配置,使得默认创建出来的table就是utf8
详见:
中的:
将本地数据库改成utf8mb4,utf8mb4_unicode_ci
(3)后来也回复了帖子:
转载请注明:在路上 » 【已解决】Spring Boot中JPA中指定生成MySQL表结构的Charset和Encoding以及Collation