Edg*_*rka 15 mysql spring jpa spring-data spring-data-jpa
我使用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
Run Code Online (Sandbox Code Playgroud)
表:
+---------------+--------------------+
| TABLE_NAME | character_set_name |
+---------------+--------------------+
| customer | utf8 |
| projects | utf8 |
| projects_data | utf8 |
+---------------+--------------------+
Run Code Online (Sandbox Code Playgroud)
Nar*_*uto 34
尝试
spring.datasource.url = jdbc:mysql://localhost:3306/gehive?useUnicode=yes&characterEncoding=UTF-8
Run Code Online (Sandbox Code Playgroud)
似乎问题是由于缺少" - ".
参考: - https://forum.hibernate.org/viewtopic.php?f=1&t=1037497&view=next
小智 6
我遇到了同样的问题,并通过将以下行添加到application.properties文件中来解决了这个问题:
spring.datasource.tomcat.connection-properties=useUnicode=true;characterEncoding=utf-8;
Run Code Online (Sandbox Code Playgroud)
注意:以下操作无效:
spring.datasource.connectionProperties=useUnicode=true;characterEncoding=utf-8;
Run Code Online (Sandbox Code Playgroud)
对于不喜欢将参数直接附加到 JDBC URL 的任何使用 HikariCP 连接池的人:
该spring.datasource.connectionProperties物业前段时间已被移除。从那时起,您需要使用连接池特定的属性,如@SebTheGreat 的回答所示:
spring.datasource.tomcat.connection-properties=useUnicode=true;characterEncoding=utf-8;
Hikari 没有connection-properties属性,但这有效:
spring.datasource.hikari.data-source-properties.useUnicode=true
spring.datasource.hikari.data-source-properties.characterEncoding=UTF-8
Run Code Online (Sandbox Code Playgroud)