1、查看mysql编码格式是否为utf-8
在/usr/local/mysql/bin路径下输入./mysql -u root -p
命令通过密码登录mysql,或者直接通过Navicat新建查询,输入show variables like '%char%'
,mysql的默认编码格式是latin1
,如果不是的话进行修改,首先在/etc/mysql路径下新建一个my.cnf文件:vim my.cnf
,再把以下内容粘贴进去:
# change in this file to the variables' values below will be ignored and
# overridden by the content of the master.info file, unless you shutdown
# the slave server, delete master.info and restart the slaver server.
# For that reason, you may want to leave the lines below untouched
# (commented) and instead use CHANGE MASTER TO (see above)
#
# required unique id between 2 and 2^32 - 1
# (and different from the master)
# defaults to 2 if master-host is set
# but will not function as a slave if omitted
#server-id = 2
#
# The replication master for this slave - required
#master-host = <hostname>
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user = <username>
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password = <password>
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port = <port>
#
# binary logging - not required for slaves, but recommended
#log-bin=mysql-bin
# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = /usr/local/mysql/data
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = /usr/local/mysql/data
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 16M
#innodb_additional_mem_pool_size = 2M
# Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size = 5M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
default-character-set=utf8
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
这段代码是网上找来的,不知道可不可行,如果配置了这个文件还不行的话(比如说像我这样的),在mysql中使用下面几句sql语句:
SET character_set_client = 'utf8';
SET character_set_results = 'utf8';
SET character_set_connection = 'utf8';
反正加完之后我的就好了
2、查看idea项目编码格式
打开项目配置
将编码格式都改成utf-8,Apply
3、在项目配置文件application.properties里配置
spring.datasource.url=jdbc:mysql://localhost:3306/项目名?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&useSSL=false
里面记得不要换行或者加空格
4、设置项目读取编码格式
idea右下角修改为utf-8,注意reload和convert的区别,前者类似预览,后者会覆盖原文件,使用时请谨慎
5、修改edit configurations
添加-Dfile.encoding=UTF-8
6、修改utf8为utf8mb4
参考文档:https://mathiasbynens.be/notes/mysql-utf8mb4#utf8-to-utf8mb4
大致意思就是,由于种种原因,mysql的utf8被弃用,后改用utf8mb4,所以上面所有的地方用到utf8的都改成utf8mb4。
7、修改数据库连接方式
无意中点开数据库属性
保存
总结
如果进行了以上所有操作最后还是乱码的话,抱歉我也帮不了你,以上就是我的毕生所学了 。持续了2天的问题,最后以这种方式解决,心情复杂。