更新时间:2022-10-04 20:34:40
[client] default-character-set = utf8mb4 [mysql] default-character-set = utf8mb4 [mysqld] character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci
所以就只能用代码解决了, 一开始是准备从JDBC连接时候就指定使用的字符集处下手.
jdbc:mysql://localhost:3306/ding?characterEncoding=UTF-8
主要把UTF-8修改为utf8mb4对于的Java Style Charset字符串应该就能解决问题吧?不过很遗憾的是, Java JDBC并不存在utf8mb4对于的字符集. 使用UTF-8的时候可以兼容urf8mb4并自动转换字符集.
For example, to use 4-byte UTF-8 character sets with Connector/J, configure the MySQL server with character_set_server=utf8mb4, and leave characterEncoding out of the Connector/J connection string. Connector/J will then autodetect the UTF-8 setting. — [MySQL:Using Character Sets and Unicode][3]
后来科普了一下, 在每一次查询请求的时候, 可以显式的指定使用的字符集, 使用 set names utf8mb4
可以指定本次链接的字符集为utf8mb4, 但这个设置在每次连接被释放后都会失效.
目前的解决办法是, 在需要插入utf8mb4的时候, 显示地调用执行 set names utf8mb4
, 如:
jdbcTemplate.execute("set names utf8mb4");
jdbcTempalte.execute("...");
,如需转载请自行联系原作者