【问题标题】:How to set collation for table attribute as utf8_bin in either annotation or application.properties file using Spring Boot如何使用 Spring Boot 在注释或 application.properties 文件中将表属性的排序规则设置为 utf8_bin
【发布时间】:2017-07-14 19:43:36
【问题描述】:

如何使用 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

【问题讨论】:

  • 有任何人解决这个问题,因为我正面临这个问题。请发表您的答案。

标签: spring-boot


【解决方案1】:

这是一个受类似问题回答启发的解决方案: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

【讨论】:

    猜你喜欢
    • 2017-06-17
    • 2016-08-24
    • 2011-06-14
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 2021-11-24
    • 2020-12-23
    • 1970-01-01
    相关资源
    最近更新 更多