java.sql.SQLException: The server time zone value xxx
-
mysql默认的时区和本地时区不一致导致的
-
有多个时区,运行时需要指定一个时区
解决方案:
方案一:修改mysql默认时区
1.查询系统支持当前的时区设置:
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.12 |
+-----------+
1 row in set (0.00 sec)
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | SYSTEM |
+------------------+--------+
2 rows in set (0.00 sec)
2.修改时区设置:
方法1:写入到配置文件需要重启mysql实例:
[mysqld]
default-time-zone=timezone
修改为
default-time-zone = '+8:00'
方法2:在线修改和查看:
mysql> set time_zone='+8:00';
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | +08:00 |
+------------------+--------+
2 rows in set (0.01 sec)
--通过now() 查看:
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | +08:00 |
+------------------+--------+
2 rows in set (0.01 sec)
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2018-08-28 16:08:38 |
+---------------------+
1 row in set (0.00 sec)
mysql> set time_zone='+0:00';
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | +00:00 |
+------------------+--------+
2 rows in set (0.01 sec)
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2018-08-28 08:09:12 |
+---------------------+
1 row in set (0.00 sec)
--自MySQL 8.0 GA版本开始支持将参数写入并且持久化:
mysql> set persist time_zone='+0:00';
--针对application 可以设置:
jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8&useOldAliasMetadataBehavior=true&autoReconnect=true&serverTimezone=UTC
--结论:
mysql> set time_zone='+8:00'; 和time_zone='system'是等同的,默认为系统时间。
方案二:连接mysql时指定时区为UTC
jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
Failed to load property source from location ‘classpath:/application.yml’
- yml的语法格式有误
application.yml:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
datasource:
type: com.alibaba.druid.pool.DruidDataSource
解决方法:修改后的application.yml:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
type: com.alibaba.druid.pool.DruidDataSource
PS:校验yml语法
注:注意缩进
- 其他问题
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
servlet:
multipart:
max-file-size: 5MB # 限制文件上传的大小
从配置文件中可以看出语法并无问题。
解决办法:
- File–>Settings–>File Encodings
这三个地方设置成UTF-8格式。重启启动项目。
- 如第一步并未解决问题,则可以用终极杀招(本人就是加了中文注释)。
删除application.yml文件中所有中文注释
spring-boot-starter-thymeleaf版本问题
spring boot默认thymeleaf版本是2.1.1
修改为:3.0.11.RELEASE就OK啦!
<properties>
<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
参考:
https://my.oschina.net/u/3740271/blog/3028874;
https://blog.csdn.net/vkingnew/article/details/82149726 ;
https://blog.csdn.net/qq_37495786/article/details/82505737;