SpringBoot 学习过程遇到的问题

java.sql.SQLException: The server time zone value xxx

  1. mysql默认的时区和本地时区不一致导致的

  2. 有多个时区,运行时需要指定一个时区

解决方案:

方案一:修改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’

  1. 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语法

注:注意缩进

  1. 其他问题
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
    username: root
    password: 123456
  servlet:
    multipart:
      max-file-size: 5MB # 限制文件上传的大小
从配置文件中可以看出语法并无问题。

解决办法:

  1. File–>Settings–>File Encodings

这三个地方设置成UTF-8格式。重启启动项目。

  1. 如第一步并未解决问题,则可以用终极杀招(本人就是加了中文注释)。

删除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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值