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

问题描述 投票:5回答:5

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

我的数据库中的unicodes会像这样保存

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

enter image description here

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

mysql spring spring-boot unicode spring-jdbc
5个回答
4
投票

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

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

4
投票

保持你的休眠配置像这样

JDBC:MySQL的://本地主机:3306 / DBNAME了useUnicode = YES&的characterEncoding = UTF-8

并改变你的数据库整理像这样

ALTER TABLE tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

更多信息:Link


1
投票

请参阅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 

1
投票

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

public class Goal{

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

}

0
投票

非答案对我有用...除了网址编码部分。

在我的案例中的解决方案是双重的:

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>

否则,其他配置不会生效。

© www.soinside.com 2019 - 2024. All rights reserved.