first:
排查原因:
打断点,查看到底是在执行存数据库操作之前就已经乱码了,还是存数据库操作后乱码的。
前者解决方案:
在web.xml里面加上:
encodingFilter org.springframework.web.filter.CharacterEncodingFilter true encoding UTF-8 在配置jdbc数据源的时候加上: encodingFilter /*
jdbc:mysql://127.0.0.1:3306/logistics?useUnicode=true&characterEncoding=UTF-8 用了POST方式提交的表单时,设置编码方式:
response.setCharacterEncoding("UTF-8");request.setCharacterEncoding("UTF-8");
存数据库操作后乱码,解决方案: 登录数据库,执行如下sql:
show variables like 'character%';
结果:
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
说明在安装数据库的时候,character_set_database,character_set_server编码格式设置错误。
解决:
找到mysql安装路径下的my.cnf(windows下为my.ini)文件在[mysqld]下添加如下两行:
character_set_server=utf8
init_connect='SET NAMES utf8'再次查看,问题解决:
| 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
转载自:https://www.cnblogs.com/tom-plus/p/5701741.html