问题描述
通过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服务关闭并重启即可。 - 再次查看字符集
重新再次保存数据,发现中文并没有乱码问题得以解决。
解决方法二
也有临时更改数据库字符集和表的方法,如下所示:
- 更改数据库test的字符集
mysql> alter database test default character set utf8;
Query OK, 1 row affected (0.00 sec)
- 更改数据库test中表tb_person的字符集
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tb_person |
+----------------+
1 row in set (0.00 sec)
## 查看表字符集
mysql> show create table tb_person;
+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tb_person | CREATE TABLE `tb_person` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(125) NOT NULL,
`age` int(10) NOT NULL,
`salary` decimal(10,2) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
## 将latin1修改为utf8
mysql> alter table tb_person default character set utf8;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0