我在后端使用spring boot 2 jpa,mysql数据库,我有一些表的字段需要保存特殊字符。我试了很多方法都没成功,
它应该非常简单,我可以看到其他人是如何使用它的,但由于某种原因,它对我来说不起作用。
像W�rtemberg一样保存的值Würtemberg
任何帮助都将不胜感激。
下面是我尝试过但没能成功的方法,
在application.properties中,
方式一:-
spring.datasource.url=jdbc:mysql://${global.database.host}:${global.database.port}/${global.database.schema}?useSSL=false&useUnicode=true&characterEncoding=UTF-8上述url中的方式2 useUnicode=yes
方式3
spring.jpa.properties.hibernate.connection.CharSet=UTF-8
spring.jpa.properties.hibernate.connection.characterEncoding=UTF-8
spring.jpa.properties.hibernate.connection.useUnicode=true方式4
spring.datasource.tomcat.connection-properties=useUnicode=true;characterEncoding=utf-8;方式5
spring.datasource.url= jdbc:mysql://localhost:3306/database?useUnicode=yes&characterEncoding=UTF-8
connection.useUnicode=true
connection.characterEncoding=utf-8
hibernate.connection.useUnicode=true
hibernate.connection.characterEncoding=UTF-8
spring.datasource.sqlScriptEncoding=UTF-8但不走运,
你知道为什么它不能工作吗,
我使用DatasourceConfiguration,如下所示:
@Slf4j
@Setter
@Configuration
public class DataSourceConfiguration {
@Value("${spring.datasource.tomcat.test-on-borrow}")
private boolean testOnBorrow;
@Value("${spring.datasource.tomcat.validation-query}")
private String validationQuery;
@Value("${global.database.host}")
private String dbHost;
@Value("${global.database.port}")
private Integer dbPort;
@Bean
@Primary
public DataSourceProperties dataSourceProperties() {
return new DataSourceProperties();
}
@Bean
@Primary
public DataSource dataSource() throws DockerException, InterruptedException {
if (StringUtils.isEmpty(dbHost) || dbPort == null) {
// some log
} else {
ServiceProbe probe = new ServiceProbe(2, TimeUnit.MINUTES);
try {
probe.probe(new IsTcpConnectionUp(dbHost, dbPort));
} catch (ServiceDownException e) {
throw new RuntimeException(e);
}
}
DataSourceProperties dsp = dataSourceProperties();
DataSourceBuilder dataSourceBuilder = dsp.initializeDataSourceBuilder();
DataSource ds = dataSourceBuilder.build();
if (ds instanceof org.apache.tomcat.jdbc.pool.DataSource) {
((org.apache.tomcat.jdbc.pool.DataSource) ds).setTestOnBorrow(testOnBorrow);
((org.apache.tomcat.jdbc.pool.DataSource) ds).setValidationQuery(validationQuery);
} else {
// some logs
}
return ds;
}
}发布于 2019-04-11 04:26:35
这是一个相当古老的问题,所以我可能只是为了历史而发布解决方案。
1)数据库排序规则和表的字段排序规则应为utf8mb4_...,例如utf8mb4_unicode_ci。
2) Application.properties连接字符串应包含:useUnicode=true&characterEncoding=UTF-8
就像你的“方式1”一样。就这样。
发布于 2020-09-02 13:05:29
默认情况下,MySQL表格字符集为latin1格式。为了存储UTF-8字符,我们需要用UTF-8字符集对表进行编码。
例如。ALTER TABLE dbName.tableName MODIFY COLUMN columName longtext CHARACTER SET utf8 COLLATE utf8_general_ci;
https://stackoverflow.com/questions/52866643
复制相似问题