问题描述:数据库字符编码为GBK,Spring Boot服务编码为UTF-8,导致服务在查询数据库的时候会出现如下异常。
代码语言:javascript复制(1267, "Illegal mix of collations (gbk_chinese_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE) for operation '='")
现象:
代码语言:javascript复制SELECT * FROM searchdicts WHERE word = 'xiao er急诊科'
SELECT * FROM searchdicts WHERE word = '消⼃'
SELECT * FROM searchdicts WHERE word = '陈㬢'
(1267, "Illegal mix of collations (gbk_chinese_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE) for operation '='")
解决方案:执行语句,强制将表的编码格式转化为UTF-8
代码语言:javascript复制alter table table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
备注:此操作有一定的风险,需要谨慎