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

Spring-Boot, Can't save unicode string in MySql using spring-data JPA

我的 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

在我的 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>

我的实体: @实体 public class DailyRashifalEntity {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String date;
private int rollno;
private String name;
//Constructors and getters/setters 
}

问题 1: 如果我使用上面的设置,我会得到异常

java.sql.SQLException: Incorrect string value: '\xE0\xA4\xA7\xE0\xA4\xBE...

问题 2: 如果我将数据源 url 更改为:

spring.datasource.url = jdbc:mysql://localhost:3306/dbname

我数据库中的 unicode 是这样保存的

 29 | 2074-03-04 |        3 | ?????????????? ?????,?????? ??????, ??????????? ????? ? ???? ???? ???? ??????  

我如何 将它们保存在Mysql中,就像它们在unicode[=46=中一样] 而不是将所有unicode数据转换????????

在您的 /etc/mysql/my.cnf 文件中更改以下内容。

[mysql]
default-character-set=utf8
[mysqld]
character-set-server=utf8

像这样保持你的休眠配置

jdbc:mysql://localhost:3306/dbname?useUnicode=yes&characterEncoding=UTF-8

并像这样更改您的数据库排序规则

ALTER TABLE tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

更多信息:Link

参见 中的 "question marks"。

此外,

⚈  spring.jpa.properties.hibernate.connection.characterEncoding=utf-8 
⚈  spring.jpa.properties.hibernate.connection.CharSet=utf-8 
⚈  spring.jpa.properties.hibernate.connection.useUnicode=true 

没有一个答案对我有用……除了 url 编码部分。

我的解决方案有两个:

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- 将此配置添加到您的调度程序配置文件中:

<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>

否则其他配置不生效

通过应用到列定义解决了这个问题,如下所示:

public class Goal{

@Column(name = "SOURCE_OF_FUNDS", length = 6000, columnDefinition = "VARCHAR(6000) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL")
private String sourceOfFunds;

}

如果你想正确保存非ascii字符,你应该确保:

  • 已创建 MySQL table with charset=encoding is UTF-8
  • Connect MySQL table with charset=encoding is UTF-8

以下是实现方法:

使用 utf-8 字符集创建 mysql table

方法一(我用的就是这个方法):在SpringBoot JPA

中配置

参考另一个 Chinese post 后,我的最终工作配置:

  • 文件: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";
  }
}

将 table 字符集设置为 utf8mb4 并将排序规则设置为 utf8mb4_unicode_ci

  • 文件:src/main/resources/application.properties
  • 配置:
spring.jpa.database-platform=com.crifan.smartelectricserver.MySQL55DialectUtf8mb4

其中:

  • com.crifan.smartelectricserver.MySQL55DialectUtf8mb4:对应上面的文件com/crifan/smartelectricserver/MySQL55DialectUtf8mb4.java

方法 2:在 MySQL 配置文件中设置 utf-8 my.cnf

在您的机器中找到您的 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'

使用 UTF-8

连接 MySQL
  • 文件:src/main/resources/application.properties
  • 配置:
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/smart_electric?useUnicode=yes&characterEncoding=UTF-8

其中:

  • useUnicode=yes&characterEncoding=UTF-8:表示连接 MySQL 与字符集 utf-8