[英]Spring-Boot, Can't save unicode string in MySql using spring-data JPA
I have my application.properties
set up like this :我的
application.properties
设置如下:
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.url = jdbc:mysql://localhost:3306/dbname?useUnicode=yes&characterEncoding=UTF-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
And In my pom.xml
I have property set up like this :在我的
pom.xml
我的属性设置如下:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<start-class>toyanathapi.Application</start-class>
<java.version>1.8</java.version>
</properties>
My entity : @Entity public class DailyRashifalEntity {我的实体:@Entity 公共类 DailyRashifalEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String date;
private int rollno;
private String name;
//Constructors and getters/setters
}
Problem 1: If I use the above setup I get the exception问题 1:如果我使用上述设置,则会出现异常
java.sql.SQLException: Incorrect string value: '\xE0\xA4\xA7\xE0\xA4\xBE...
Problem 2 : If I change the datasource url into this :问题 2:如果我将数据源 url 更改为:
spring.datasource.url = jdbc:mysql://localhost:3306/dbname
The unicodes in my database get saved like this我的数据库中的 unicodes 像这样保存
29 | 2074-03-04 | 3 | ?????????????? ?????,?????? ??????, ??????????? ????? ? ???? ???? ???? ??????
How can I save them in Mysql like they are in unicode instead of getting all the unicode data converted into ????????
如何将它们保存在 Mysql 中,就像它们在unicode 中一样,而不是将所有 unicode 数据转换为
????????
. .
In your /etc/mysql/my.cnf
file change the following.在您的
/etc/mysql/my.cnf
文件中更改以下内容。
[mysql]
default-character-set=utf8
[mysqld]
character-set-server=utf8
Keep your hibernate configuration Like this保持你的休眠配置像这样
jdbc:mysql://localhost:3306/dbname?useUnicode=yes&characterEncoding=UTF-8
jdbc:mysql://localhost:3306/dbname?useUnicode=yes&characterEncoding=UTF-8
And Change your DB Collation Like this并像这样更改您的数据库整理
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
See "question marks" in http://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored .请参阅http://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored 中的“问号”。
Also,还有,
⚈ spring.jpa.properties.hibernate.connection.characterEncoding=utf-8
⚈ spring.jpa.properties.hibernate.connection.CharSet=utf-8
⚈ spring.jpa.properties.hibernate.connection.useUnicode=true
Solved it by applying to the column definition as shown below:通过应用于列定义来解决它,如下所示:
public class Goal{
@Column(name = "SOURCE_OF_FUNDS", length = 6000, columnDefinition = "VARCHAR(6000) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL")
private String sourceOfFunds;
}
if you want save non-ascii character correctly, you should make sure:如果您想正确保存非 ascii 字符,您应该确保:
following is how to achieve it:以下是如何实现它:
after refer another Chinese post , my final working configuration:参考另一篇中文帖子后,我的最终工作配置:
src/main/java/com/crifan/smartelectricserver/MySQL55DialectUtf8mb4.java
src/main/java/com/crifan/smartelectricserver/MySQL55DialectUtf8mb4.java
package com.crifan.smartelectricserver;
import org.hibernate.dialect.MySQL55Dialect;;
// import org.hibernate.dialect.MySQL5InnoDBDialect; // Deprecated
// public class MySQL5InnoDBDialectUtf8mb4 extends MySQL5InnoDBDialect {
public class MySQL55DialectUtf8mb4 extends MySQL55Dialect {
@Override
public String getTableTypeString() {
return "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci";
}
}
which set table charset to utf8mb4
and collation to utf8mb4_unicode_ci
将表字符集设置为
utf8mb4
并将排序规则设置为utf8mb4_unicode_ci
and和
src/main/resources/application.properties
src/main/resources/application.properties
spring.jpa.database-platform=com.crifan.smartelectricserver.MySQL55DialectUtf8mb4
in which:其中:
com.crifan.smartelectricserver.MySQL55DialectUtf8mb4
: corresponding above file com/crifan/smartelectricserver/MySQL55DialectUtf8mb4.java
com.crifan.smartelectricserver.MySQL55DialectUtf8mb4
: 对应上面的文件com/crifan/smartelectricserver/MySQL55DialectUtf8mb4.java
my.cnf
my.cnf
设置utf-8 find your my.cnf
in your machine, edit to:在您的机器中找到您的
my.cnf
,编辑为:
[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
init_connect='SET NAMES utf8mb4'
src/main/resources/application.properties
src/main/resources/application.properties
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/smart_electric?useUnicode=yes&characterEncoding=UTF-8
in which:其中:
useUnicode=yes&characterEncoding=UTF-8
: means connect MySQL with charset utf-8 useUnicode=yes&characterEncoding=UTF-8
: 表示用字符集 utf-8 连接 MySQLNon of the answer worked for me … except the url encoding part.没有一个答案对我有用……除了 url 编码部分。
The solution in my case is twofold:在我的情况下,解决方案是双重的:
1- Add encoding in the URL of Database config bean: 1- 在数据库配置 bean 的 URL 中添加编码:
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/customersdb?useSSL=false&useUnicode=yes&characterEncoding=UTF-8&characterSetResults=UTF-8" />
2- Add this configuration to your dispatcher config file: 2- 将此配置添加到您的调度程序配置文件中:
<filter>
<filter-name>encoding-filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding-filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
Otherwise, other configurations don't take effect.否则其他配置不生效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.