在springboot中,使用MyBatis插入datetime类型的数据,但是发现控制台打印时间正常,但是插入mysql数据库里面时间刚好相差了8小时:
也就是说插入mySql数据库时,数据库里的时间固定少了8个小时。
后来发现原来是时区的问题,在application.properties配置连接数据库的时候,我直接后面写的是serverTimezone=UTC,这是网上看到的资料,能用我就没在意,是这样的:
spring.datasource.url=jdbc:mysql://localhost:3306/lmg-test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&zeroDateTimeBehavior=convertToNull
1
对于中国来说,只需要将serverTimezone的值改为GMT%2B8就好了,也就是后缀上应为serverTimezone=GMT%2B8,如下所示:
spring.datasource.url=jdbc:mysql://localhost:3307/lmg-test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull
转载地址:https://blog.csdn.net/weixin_42322648/article/details/105116600
————————————————
版权声明:本文为CSDN博主「小四是个程序员」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42322648/java/article/details/105116600