MySQL spring 数据 jpa utf-8 编码不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38677740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
spring data jpa utf-8 encoding not working
提问by Edgaras Karka
I use spring-data-jpa
and mysql
database. My tables character set is utf-8. Also I added ?useUnicode=yes&characterEncoding=utf8
to mysql url in application.properties file. Problem when I pass characters like "?????" to controller to save it in mysql. In mysql I got ??? marks. But when I use mysql console example update projects_data set data="?????" where id = 1;
every works well.
我使用spring-data-jpa
和mysql
数据库。我的表格字符集是 utf-8。我?useUnicode=yes&characterEncoding=utf8
还在 application.properties 文件中添加到 mysql url。当我传递像“?????”这样的字符时出现问题 到控制器以将其保存在 mysql 中。在 mysql 我得到了???分数。但是当我使用 mysql 控制台示例时,update projects_data set data="?????" where id = 1;
每个都运行良好。
application.properties:
应用程序属性:
# "root" as username and password.
spring.datasource.url = jdbc:mysql://localhost:3306/gehive?useUnicode=yes&characterEncoding=utf8
spring.datasource.username = gehive
spring.datasource.password = pass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
tables:
表:
+---------------+--------------------+
| TABLE_NAME | character_set_name |
+---------------+--------------------+
| customer | utf8 |
| projects | utf8 |
| projects_data | utf8 |
+---------------+--------------------+
回答by Naruto
Try
尝试
spring.datasource.url = jdbc:mysql://localhost:3306/gehive?useUnicode=yes&characterEncoding=UTF-8
It seems issue is due to missing "-".
似乎问题是由于缺少“-”。
Reference:- https://forum.hibernate.org/viewtopic.php?f=1&t=1037497&view=next
参考:- https://forum.hibernate.org/viewtopic.php?f=1&t=1037497&view=next
回答by SebTheGreat
I had the same issues, and I solved it by adding this line to my application.properties
file:
我遇到了同样的问题,我通过将这一行添加到我的application.properties
文件中来解决它:
spring.datasource.tomcat.connection-properties=useUnicode=true;characterEncoding=utf-8;
Note:The following didn'twork:
注意:下面没有工作:
spring.datasource.connectionProperties=useUnicode=true;characterEncoding=utf-8;
回答by Joel
For anyone using the HikariCP connection pool who prefers not to append the parameters directly to the JDBC URL:
对于不喜欢将参数直接附加到 JDBC URL 的任何使用 HikariCP 连接池的人:
The spring.datasource.connectionProperties
property was removed some time ago. Since then, you need to use connection pool specific properties as shown in @SebTheGreat's answer:
spring.datasource.tomcat.connection-properties=useUnicode=true;characterEncoding=utf-8;
该spring.datasource.connectionProperties
物业前段时间已被移除。从那时起,您需要使用连接池特定的属性,如@SebTheGreat 的回答所示:
spring.datasource.tomcat.connection-properties=useUnicode=true;characterEncoding=utf-8;
Hikari doesn't have a connection-properties
property, but this works:
Hikari 没有connection-properties
属性,但这有效:
spring.datasource.hikari.data-source-properties.useUnicode=true
spring.datasource.hikari.data-source-properties.characterEncoding=UTF-8
回答by vijay
Remember to escape any special characters, as below:
spring.datasource.url=jdbc\:mysql\://localhost\:3306/${SERVER_DB_NAME}\?useUnicode=true\&characterEncoding=utf\-8\&characterSetResults=utf\-8
请记住转义任何特殊字符,如下所示:
spring.datasource.url=jdbc\:mysql\://localhost\:3306/${SERVER_DB_NAME}\?useUnicode=true\&characterEncoding=utf\-8\&characterSetResults=utf\-8
回答by hugo blanc
In my case that's solve my issue https://mathiasbynens.be/notes/mysql-utf8mb4
就我而言,这解决了我的问题 https://mathiasbynens.be/notes/mysql-utf8mb4
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
回答by Quang Huynh
It works for me if using mysql java connector version 5.1.44 (before I used connector 8.0.17 it doesn't work)
如果使用 mysql java 连接器版本 5.1.44,它对我有用(在我使用连接器 8.0.17 之前它不起作用)