![](/img/trans.png)
[英]Can't connect a spring-boot 2 service to mysql in a different container
[英]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>
我的实体:@Entity 公共类 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
我的数据库中的 unicodes 像这样保存
29 | 2074-03-04 | 3 | ?????????????? ?????,?????? ??????, ??????????? ????? ? ???? ???? ???? ??????
如何将它们保存在 Mysql 中,就像它们在unicode 中一样,而不是将所有 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;
更多信息: 链接
还有,
⚈ spring.jpa.properties.hibernate.connection.characterEncoding=utf-8
⚈ spring.jpa.properties.hibernate.connection.CharSet=utf-8
⚈ spring.jpa.properties.hibernate.connection.useUnicode=true
通过应用于列定义来解决它,如下所示:
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 字符,您应该确保:
以下是如何实现它:
参考另一篇中文帖子后,我的最终工作配置:
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
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'
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
: 表示用字符集 utf-8 连接 MySQL没有一个答案对我有用……除了 url 编码部分。
在我的情况下,解决方案是双重的:
1- 在数据库配置 bean 的 URL 中添加编码:
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/customersdb?useSSL=false&useUnicode=yes&characterEncoding=UTF-8&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>
否则其他配置不生效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.