简体   繁体   English

Spring-Boot,无法使用 spring-data JPA 在 MySql 中保存 unicode 字符串

[英]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;

More information : Link更多信息: 链接

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 字符,您应该确保:

  • Created MySQL table with charset=encoding is UTF-8使用 charset=encoding创建的MySQL 表是 UTF-8
  • Connect MySQL table with charset=encoding is UTF-8使用 charset=encoding连接MySQL 表是 UTF-8

following is how to achieve it:以下是如何实现它:

Create mysql table with utf-8 charset使用 utf-8 字符集创建 mysql 表

method 1 (I use this method): config in Spring Boot JPA方法一(我用这个方法):在Spring Boot JPA中配置

after refer another Chinese post , my final working configuration:参考另一篇中文帖子后,我的最终工作配置:

  • file: src/main/java/com/crifan/smartelectricserver/MySQL55DialectUtf8mb4.java文件: src/main/java/com/crifan/smartelectricserver/MySQL55DialectUtf8mb4.java
  • code:代码:
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

  • file: src/main/resources/application.properties文件: src/main/resources/application.properties
  • config:配置:
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

method 2: set utf-8 in MySQL config file my.cnf方法二:在MySQL配置文件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'

Connect MySQL with UTF-8使用 UTF-8 连接 MySQL

  • file: src/main/resources/application.properties文件: src/main/resources/application.properties
  • config:配置:
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 连接 MySQL

Non 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&amp;useUnicode=yes&amp;characterEncoding=UTF-8&amp;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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM