学习spring boot时要在windows本地安装mysql5.7,配置好之后项目,启动之后提示需要设置时区,并且在使用过程中发现出现乱码,中文无法显示,出现上述问题的主要是mysql5.7中没有设置时区和编码。
推荐:《mysql教程》
解决方案
1、在spring boot的配置文件application.properties中设置mysql的数据源url
spring.datasource.url=jdbc:mysql://localhost:3306/yunzhi_spring_boot?characterencoding=utf-8&servertimezone=gmt%2b8
解释:
characterencoding为设置数据库编码,采用utf-8;
servertimezone为设置时区,“gmt%2b8”即gmt+8,东八区北京时间;
有兴趣可以了解一下gmt和utc的区别。
2、如果每次都这样设置,感觉好麻烦,直接修改mysql的配置文件my.ini,设置两个参数:
default-time-zone=+08:00character-set-server=utf8
然后保存,重启mysql服务。
项目中的mysql url就可以简化为:
spring.datasource.url=jdbc:mysql://localhost:3306/yunzhi_spring_boot
以上就是mysql如何设置时区和默认编码的详细内容。