lvxianchao / notes

狗屁不通瞎写的博客
https://coderlxc.com
1 stars 0 forks source link

指定 Spring Data JPA 通过 Entity 创建数据表时使用 UTF8MB4 字符集 #47

Open lvxianchao opened 1 year ago

lvxianchao commented 1 year ago

默认情况下 Spring Data JPA 通过 Entity 创建的数据表的字符集是 latin, 通常我都是用 utf8mb4 字符集,可以通过以下操作实现。

application.yml 里添加一个配置项用于指定一个我们自定义的 Dialect 类(为了方便以下写为 property 的格式,如果是 yml 格式需要自行调整):

spring.jpa.properties.hibernate.dialect: com.lvxianchao.translation.config.MySQL5UTF8mb4Dialect

config 包下创建 MySQL5UT8MB4Dialect,其内容如下:

package com.lvxianchao.translation.config;

import org.hibernate.dialect.MySQLDialect;

/**
 * Author: lvxianchao
 * Date: 2023-01-12 17:36
 * Description:
 */
public class MySQL5UTF8mb4Dialect extends MySQLDialect {

    @Override
    public String getTableTypeString() {
        return "engine = innodb default charset = utf8mb4";
    }

}