【问题标题】:Spring-Boot, Can't save unicode string in MySql using spring-data JPASpring-Boot,无法使用 spring-data JPA 在 MySql 中保存 unicode 字符串
【发布时间】:2017-11-19 03:51:28
【问题描述】:

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

我的实体: @实体 公共类 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:如果我将数据源网址更改为:

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

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

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

如何我可以在 Mysql 中保存它们,就像它们在 unicode 而不是获取所有 unicode 数据已转换????????

【问题讨论】:

  • 删除数据库并重试。你可以设置spring.jpa.hibernate.ddl-auto = create进行测试。
  • 您是否也尝试过在连接上设置字符集?
  • @premkumar 不走运..
  • @FMashiro 最近几天我开始春天了,我不太确定你说的是什么感觉。你能解释一下吗?
  • show create database dbname 粘贴输出

标签: mysql spring spring-boot unicode spring-jdbc


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    像这样保持你的休眠配置

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

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

    ALTER TABLE tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    

    更多信息:Link

    【讨论】:

    • 我的项目遇到了与您描述的相同的问题。这对我有用。
    • 问题不在我的项目中,而是在 Mechine 的 mysql 配置中
    【解决方案3】:

    请参阅http://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored 中的“问号”。

    还有,

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

    【讨论】:

      【解决方案4】:

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

      我的解决方案是双重的:

      1- 在 Database config 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>
      

      否则其他配置不生效。

      【讨论】:

        【解决方案5】:

        通过应用到列定义来解决它,如下所示:

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

        【讨论】:

          【解决方案6】:

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

          • 已创建 charset=encoding 为 UTF-8 的 MySQL 表
          • 连接 charset=encoding 为 UTF-8 的 MySQL 表

          以下是如何实现它:

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

          方法一(我用这个方法):在Spring Boot 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";
            }
          }
          

          将表字符集设置为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配置文件my.cnf中设置utf-8

          在您的机器中找到您的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&amp;characterEncoding=UTF-8: 表示用charset utf-8连接MySQL

          【讨论】:

            猜你喜欢
            • 2014-10-21
            • 2015-06-13
            • 1970-01-01
            • 1970-01-01
            • 2021-01-28
            • 2016-10-11
            • 2016-12-02
            • 2018-08-06
            • 2017-05-09
            相关资源
            最近更新 更多