SpringBoot存入数据乱码问题:
今天遇到一个乱码问题,记录一下
在springboot应用中我保存的所有字符串都在数据库中显示 ?
起初我以为是数据库编码问题,查询后发现springboot与mysql表中设置的均为utf8
百度后才知道是数据库中的
character-set-server 属性默认设置的是latin1,需将其修改为utf8
-
在数据库中输入查询信息:
show VARIABLES like "char%"
查询得到的结果为:
当然这是我修改后的,修改前character-set-server 的value是latin1
-
找到自己的mysql文件夹
最初我文件夹下没有my.ini,只有my-default.ini
首先将my-default.ini其中内容修改为
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/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
# 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 = .....
# 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
之后再copy一份重命名为my.ini
-
重启mysql
使用管理员模式下的cmd
输入net stop mysql
会显示停止mysql
再输入net start mysql
显示启动mysql
net stop mysql**
会显示停止mysql
再输入net start mysql
显示启动mysql
完成这两个操作后,重新执行代码即可