今天在本地搭建環(huán)境之后,app端寫(xiě)入后臺(tái)發(fā)現(xiàn)中文無(wú)法正確寫(xiě)入截型,全部顯示為???趴荸,摸索了一會(huì),設(shè)置過(guò)tomcat環(huán)境也設(shè)置過(guò)數(shù)據(jù)庫(kù)字符集宦焦,結(jié)果還是不行发钝,后來(lái)咨詢(xún)大佬們,嘗試如下:
先在數(shù)據(jù)庫(kù)中查詢(xún)字符編碼:
show VARIABLES like 'char%';
結(jié)果如下:
+--------------------------+----------------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | J:\mysql-5.7.16-winx64\share\charsets\ |
+--------------------------+----------------------------------------+
發(fā)現(xiàn)character_set_server
這是個(gè)叛徒波闹!原因是MySQL的默認(rèn)編碼是拉丁酝豪,所以打開(kāi)mysql的安裝目錄,找到my.ini精堕,在[mysqld]最后下面加上
character_set_server=utf8
init_connect='SET NAMES utf8'
之后重啟MySQL寓调。
再查看一下編碼,結(jié)果變?yōu)椋?/p>
+--------------------------+----------------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | J:\mysql-5.7.16-winx64\share\charsets\ |
敵軍被干掉了锄码,測(cè)試也一切正常了~