最近在写一个项目的时候,用了maven仓库里面较新的mysql的JDBC驱动,debug时总是报路径错误,尝试了各种方法,却还是报一样的错误,代码在其他正常的工具上可以正常运行,
application.yml的全局配置是这么写的:
server:
port: 8081
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/aaa
username: root
password: root
但是却发现报错了,错误原因是:
Error querying database. Cause: 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.
这是因为在访问数据库时无法识别时区,所以我们需要把JDBC的url值改为这样:
url: jdbc:mysql://localhost:3306/aaa?characterEncoding=utf-8&serverTimezone=UTC
这里要注意了,我们编译一下项目发现出现了这样的错误:
Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 119; 对实体 "serverTimezone" 的引用必须以 ';' 分隔符结尾。
这里其实是因为xml把&作为一个特殊符号处理了,所以我们需要把&替换为&这样就不会报错了。
url: jdbc:mysql://localhost:3306/aaa?characterEncoding=utf-8&serverTimezone=UTC
解决了这个问题之后,我们继续回到配置文件,我们再编译一下项目,这次错误倒是没有了,但是还有一些警告,虽然我们一般忽略警告,但是看起来还是挺不舒服的,所以解决一下吧。其中一个警告是这样的:
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.
这是因为mysql的JDBC驱动使用了新的包名,所以我们需要将driver的值改为com.mysql.cj.jdbc.Driver
driver: com.mysql.cj.jdbc.Driver
还有一个警告是这样的:
Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
因为新版本的MySQL要求是否进行ssl连接,所以我们需要设置useSSL=false或者useSSL=true。
url: dbc:mysql://localhost:3306/aaa?characterEncoding=utf-8&serverTimezone=UTC&useSSL=false