為了兼容emoji表情符及一些特殊字符(4字節utf保存的字符,若采用傳統的uft8的3字節保存會導致保存報錯),需要在項目中設置uft8mb4編碼格式。
這里首先看一下mysql官網文檔對於使用的mysql驅動的說明:https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-charsets.html 對於驅動版本有明確要求。
Notes For Connector/J 5.1.46 and earlier: In order to use the utf8mb4 character set for the connection,
the server MUST be configured with character_set_server=utf8mb4; if that is not the case,
when UTF-8 is used for characterEncoding in the connection string, it will map to the MySQL character set name utf8,
which is an alias for utf8mb3.
對於5.1.46及更早的版本,要使用utf8mb4字符集進行連接,服務器必須配置character_set_server=utf8mb4;如果不是這樣,
則在連接字符串中使用UTF-8進行字符編碼時,它將映射到MySQL字符集名稱utf8,這是utf8mb3的別名。 For Connector/J 5.1.47 and later: When UTF-8 is used for characterEncoding in the connection string, it maps to the MySQL character set name utf8mb4.
當UTF-8用於連接字符串中的字符編碼時,它將映射到MySQL字符集名稱utf8mb4。 If the connection option connectionCollation is also set alongside characterEncoding and is incompatible with it,
characterEncoding will be overridden with the encoding corresponding to connectionCollation. Because there is no Java-style character set name for utfmb3 that you can use with the connection option charaterEncoding,
the only way to use utf8mb3 as your connection character set is to use a utf8mb3 collation (for example, utf8_general_ci) for the connection option connectionCollation,
which forces a utf8mb3 character set to be used, as explained in the last bullet.
由於utfmb3沒有可用於連接選項charatencoding的Java樣式字符集名稱,
因此使用utf8mb3作為連接字符集的唯一方法是對連接選項connection collation使用utf8mb3排序規則(例如,utf8_general_ci),
這將強制使用utf8mb3字符集。
另外:
- mysql的版本不能太低,低於5.5.3的版本不支持utf8mb4編碼。
- 若出現仍未生效的情況,可考慮修改mysql數據庫對於字符的配置,以及相應的表對於字符的配置。
大致需要修改的配置如下:
- mysql配置文件:
[client] default-character-set=utf8mb4 [mysql] default-character-set=utf8mb4 [mysqld] character-set-server=utf8mb4 collation-server=utf8mb4_general_ci
- 建庫時,指定字符集編碼為 utf8mb4、字符集校對規則為 utf8mb4_general_ci 或 utf8mb4_unicode_ci。
- 建表時指定 utf8mb4 編碼。