问题描述
通过debug一个数据库保存的实体对象,controller层映射实体并没有乱码如下图所示:
最终保存到数据库里是以???
保存到数据库中的。
很清晰可以看出并不是后台中文乱码,而是保存到数据库中出现了乱码。
解决方法
- 查看mysql的字符集
show variables like '%char%';
查询出的字符集结果如下所示:
将上图中的character_set_server
设置为utf8
即可。
- 设置mysql的字符集
由于新版mysql并没有存在/etc/my.cnf
这个文件,所以在/etc/
路径下创建my.cnf文件并设置如下内容即可。
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[client]
default-character-set=utf8
[mysqld]
character-set-server=utf8
[mysqldump]
user=root
password=123456
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
- 重启mysql
找到系统偏好设置
将mysql服务关闭并重启即可。 - 再次查看字符集
重新再次保存数据,发现中文并没有乱码问题得以解决。