使用Spring Boot连接数据库的坑

Spring Boot连接数据库时有许多的坑

一.时区问题

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.

只需要在url配置后面加上serverTimezone=GMT%2B8就可以了,这是由于mysql默认时区是美国,和我们差8个小时,也可以加上serverTimezone=UTC,直接将时区设置为美国

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.datasource.url=jdbc:mysql://localhost:3306/scott?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.url=jdbc:mysql://localhost:3306/scott?useUnicode=true&characterEncoding=utf-8&&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root

二.驱动过时问题

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,只需要将驱动修改下就可以了

#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.datasource.url=jdbc:mysql://localhost:3306/scott?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.url=jdbc:mysql://localhost:3306/scott?useUnicode=true&characterEncoding=utf-8&&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root

猜你喜欢

转载自blog.csdn.net/progammer10086/article/details/86595386