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

Sai*_*bid 6 spring-boot

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

我尝试过很多方法但是没有用.你能帮我吗?

我尝试了以下方法.

第一:使用@Column注释,如下所示:

@Column(name = "user_id",columnDefinition="VARCHAR(255) COLLATE utf8_bin")
Run Code Online (Sandbox Code Playgroud)

第二:

 @Column(columnDefinition="VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_bin")
Run Code Online (Sandbox Code Playgroud)

第三:使用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
Run Code Online (Sandbox Code Playgroud)

第四:

spring.datasource.url =jdbc:mysql://localhost:3306/iot_schema?createDatabaseIfNotExist=true&useUnicode=true&connectionCollation=utf8_bin&characterSetResults=utf8
Run Code Online (Sandbox Code Playgroud)

Mar*_*nBG 6

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

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

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

将该类设置为在application.properties使用

spring.jpa.properties.hibernate.dialect=my.package.MySQLCustomDialect
Run Code Online (Sandbox Code Playgroud)

这是生成的 SQL 查询:

spring.jpa.properties.hibernate.dialect=my.package.MySQLCustomDialect
Run Code Online (Sandbox Code Playgroud)