记录:spring-boot+mybatis+mysql 查询数据时间与返回数据时间不一致

tech2025-08-12  12

     开发中遇到一个很奇怪的事,同样的时间参数通过数据库工具可以查到数据,但是接口查不到,就算通过其他参数查询能返回数据,时间相差8个小时左右, 其中数据类型为 datetime;

     查看数据库时区如下:

                               

   可以看出时区不对,我国时区是GMT+8

解决办法如下:

    在数据库连接配置文件中添加 &serverTimezone=GMT%2B8

      

spring.datasource.mg.url=jdbc:mysql://ip:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2b8&useSSL=false spring.datasource.mg.username=test spring.datasource.mg.password=test spring.datasource.mg.driver-class-name=com.mysql.cj.jdbc.Driver
最新回复(0)