1. 錯誤描述
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### 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.
### The error may exist in mapper/UserMapper.xml
### The error may involve com.day01_1.pojo.selectbyid
### The error occurred while executing a query
### Cause: java.sql.SQLException
2. 錯誤原因
(1)服務器時區值不可識別,應該在MySQL連接字符串後面加上屬性:serverTimezone。
JDBC連接Mysql8時應該使用com.mysql.cj.jdbc.Driver驅動, 並且需要指定時區serverTimezone。
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC" />
<property name="username" value="root" />
<property name="password" value="lrk" />
</dataSource>
(2)在設定時區的時候,如果設定serverTimezone=UTC,會比中國時間早8個小時,如果在中國,可以選擇Hongkong。
<property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=Hongkong" />
3. 其他常見的錯誤及解決方案
(1)有些版本的MySQL如果未設置顯式選項,則必須默認建立SSL連接,爲了符合不使用SSL的現有應用程序,您可以將verifyServerCertificate屬性設置爲false,您需要通過設置useSSL=false顯式禁用SSL,或者設置useSSL=true並提供用於服務器證書驗證的信任庫
解決方案:在MySQL連接數據庫字符串後加上參數&useSSL=false。
jdbc:mysql://localhost:3306/mail_server?useSSL=false&serverTimezone=UTC
<!--xml文件的&需要轉義成&,如果是字符串配置則不需要,直接用&-->
(2)xml中配置錯誤,例如需要轉義的字符沒有轉義,如下:
jdbc:mysql://localhost:3306/mail_server?useSSL=false&serverTimezone=UTC
解決方案:將&serverTimezone=UTC替換爲&serverTimezone=UTC,如下:
jdbc:mysql://localhost:3306/mail_server?useSSL=false&serverTimezone=UTC