springboot连接JDBC及application.yml的注意事项

springboot连接JDBC及application.yml的注意事项


刚接触springboot的我在网上随便找了个 案例跟着做,结果就出现以下的报错信息:

Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class

Action:
Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

于是乎,我在网上找了很多资料了解原因,才发现我的application.yml文件用了 tab 做缩进,然而application.yml文件不能用 tab 做缩进,所以我将缩进改用 空格 ,重新运行,就出现以下报错信息:

Loading class `com.mysql.jdbc.Driver'. 
This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'.
 The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

它说“com.mysql.jdbc.driver”这个类已经被否决,新的驱动程序类是’com.mysql.cj.jdbc.driver’。不过驱动程序是通过SPI自动注册,所以通常不需要手动加载驱动程序类。
于是我将application.yml配置改为:

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/tongxinyiban

又出现如下报错信息:

Failed to obtain JDBC Connection; 
nested exception is java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. 
You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

以上说的是时区问题,要配置serverTimezone属性才可以解决。于是乎,我直接将大部分常见的属性加了上去,如下:

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/tongxinyiban?allowMultiQueries=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai

终于成功了,数据也可以查出来了!

最后,注意事项:

  1. application.yml的缩进应该用 tab ,而不是 空格
  2. application.yml的注释应该用 # ,而不是 //
  3. 冒号后面必须有空格,否则会报错;
  4. 如果报错总是找不到原因,就自己打一遍,毕竟符号什么的小错误是很难看出的。

你可能感兴趣的:(java,springboot)