Java timezone設置和mybatis連接數據庫時區設置
JVM時區設置
springboot工程運行時,需要指定時區,這樣獲取的時間才會和系統時間相同。以下介紹方法:
1.查看當前時區
centos7以前(不含centos7)
cat /etc/sysconfig/clock
centos7以后(包含centos7)
timedatectl
在啟動jar包的時候指定-Duser.timezone即可。舉例:
nohup java -Xms1024m -Xmx10240m -Xmn3840m -Xss512k -Djava.io.tmpdir=./tmp-jar -Duser.timezone=Asia/Shanghai $JAR_NAME >/dev/null 2>&1 &
Mybatis
連接MYSQL數據庫時需要指定時區,這樣查出來的時間和數據庫中的時間才會一致。
具體操作:
spring.datasource.druid.url=jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useSSL=false&zeroDateTimeBehavior=convertToNull&useLegacyDatetimeCode=false
注意這里的 serverTimezone=Asia/Shanghai
這里使用的是阿里的druid連接池。
Mybatis timezone問題
Mybatis 連接報錯
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=GMT%2B8
<property name="url" value="jdbc:mysql://localhost:3306/database?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8"/>
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://enjoy-day.blog.csdn.net/article/details/109044134