gpt4 book ai didi

spring-boot - 如何使用 Spring Boot 在注释或 application.properties 文件中将表属性的排序规则设置为 utf8_bin

转载 作者:行者123 更新时间:2023-12-03 12:24:16 24 4
gpt4 key购买 nike

如何使用 Spring Boot 在注释或 application.properties 文件中将表属性的排序规则设置为 utf8_bin?

我尝试了很多方法,但没有奏效。你能帮我吗?

我尝试了以下方法。

第一:像这样使用@Column 注释:

@Column(name = "user_id",columnDefinition="VARCHAR(255) COLLATE utf8_bin")

第二:
 @Column(columnDefinition="VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_bin")

第三:使用application.properties文件
spring.jpa.properties.hibernate.connection.characterEncoding=utf-8
spring.jpa.properties.hibernate.connection.CharSet=utf-8
spring.jpa.properties.hibernate.connection.useUnicode=true
spring.jpa.properties.hibernate.connection.collationConnection=utf8_bin

第四:
spring.datasource.url =jdbc:mysql://localhost:3306/iot_schema?createDatabaseIfNotExist=true&useUnicode=true&connectionCollation=utf8_bin&characterSetResults=utf8

最佳答案

这是一个受类似问题答案启发的解决方案:Set Table character-set/collation using Hibernate Dialect?

扩展首选的 MySQL 方言并覆盖其 getTableTypeString() 方法,如下所示:

public class MySQLCustomDialect extends MySQL8Dialect {
@Override
public String getTableTypeString() {
return " ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin";
}
}

将该类设置为在 application.properties 中使用:
spring.jpa.properties.hibernate.dialect=my.package.MySQLCustomDialect

这是生成的 SQL 查询:

    create table test_table (
...
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin

关于spring-boot - 如何使用 Spring Boot 在注释或 application.properties 文件中将表属性的排序规则设置为 utf8_bin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42430786/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com