在spring项目中,我尝试将西里尔字符引入到数据库中。但是数据库不会对其进行编码。
我使用了扩展的JpaRepository接口,方法save (T t),当我发送英文文本时,一切都正常工作;
当程序尝试用西里尔字母保存实体时,我得到异常"Invalid string value:'\ xD0 \ xA5 \ xD0 \ xB0 \ xD0 \ xB1 ...“对于这一列...“
所以编码不起作用。
我的数据库字符变量:
application.properties:
launchMode=cli
#Database settings
spring.datasource.url=jdbc:mysql://localhost:3306/mySecondBD?serverTimezone=UTC
spring.jpa.hibernate.ddl-auto=update
spring.datasource.username=root
spring.datasource.password=password
#spring.datasource.driver-class-name=org.gjt.mm.mysql.Driver
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.tomcat.connection-properties=useUnicode=true;characterEncoding=utf-8;
spring.datasource.sql-script-encoding=UTF-8
问题:
哪里需要设置更多的字符集编码参数?
发布于 2018-06-22 09:00:43
编辑属性:
spring.datasource.url=jdbc:mysql://localhost:3306/mySecondBD?useUnicode=yes&characterEncoding=UTF-8
检查你的变量:
mysql> show variables like 'char%';
编辑my.cnf:
vi /etc/my.cnf
[client]
default-character-set=utf8
最后,重新检查MySQL变量并确认查询结果。
发布于 2018-06-27 12:01:46
十六进制D0A5D0B0D0B1
是西里尔字母Хаб
的UTF8编码。因此,使用MySQL的utf8 (或utf8mb4)应该可以。
当您说'\ xD0 \ xA5 \ xD0 \ xB0 \ xD0 \ xB1 ..."
时,您有额外的空格;它们在您看到的输出中吗?
请提供SHOW CREATE TABLE page
。
不知何故,您需要说明客户端编码是utf8。一种方法(在春季)是将此代码放入application.yml
中
datasource:
connectionInitSql: "SET NAMES 'utf8'"
发布于 2018-06-27 12:10:01
使用了这个查询,它对我很有效...ALTER TABLE varchar MODIFY COLUMN column_name
varchar(255)
CHARACTER SET utf8 COLLATE utf8\_general\_ci;
https://stackoverflow.com/questions/50978129
复制相似问题