關(guān)于SpringBoot mysql數(shù)據(jù)庫時區(qū)問題
后端開發(fā)中常見的幾個時區(qū)設(shè)置
第一個設(shè)置點配置文件 spring.jackson.time-zone
第二個設(shè)置點 高版本SpringBoot版本 mysql-connector-java 用的是8.X,mysql8.X的jdbc升級了,增加了時區(qū)(serverTimezone)屬性,并且不允許為空。
第三個設(shè)置點 mysql time_zone變量
詞義serverTimezone臨時指定mysql服務(wù)器的時區(qū)
spring.jackson.time-zone 設(shè)置spring默認時區(qū)
system_time_zone mysql服務(wù)器時區(qū) ,time_zone默認System追隨system_time_zone
幾種情況1、time_zone 為 System,serverTimezone為GMT+8,jackson.time-zone未定義
插入情況
再查詢此條記錄
個人覺得Spring默認時區(qū)為格林尼治時區(qū),web服務(wù)器當(dāng)前時區(qū)為東八區(qū),進行加8操作。
2、set GLOBAL time_zone = ’+3:00’,serverTimezone為GMT+8,jackson.time-zone為GMT+8
createTime 為 timestamp類型
修改配置后,需要重啟SpringBoot
新增情況
數(shù)據(jù)庫中顯示
查詢記錄
個人理解,serverTimezone設(shè)置覆蓋掉了mysql的time_zone變量,跟SpringBoot回話時區(qū)還是東8
3、上述環(huán)境,不重啟SpringBoot,直接改變time_zone = ’+5:00’
改變后,上條記錄往后調(diào)整2小時。
SpringBoot查詢,一樣
說明,timeStamp類型存儲的是格林尼治時間,加上time_zone時區(qū)
當(dāng)time_zone變化時,回話沒結(jié)束,serverTimeZone東8還是對應(yīng)time_zone的東3
SpringBoot插入
個人理解,serverTimeZone東8 還是和 time_zone 東3對應(yīng),但是插入發(fā)現(xiàn) 當(dāng)前time_zone已經(jīng)改成東5,就加2小時。
重啟SpringBoot,重新查詢
雖然,mysql變量time_zone為+5,但是重啟后,serverTimeZone直接覆蓋,設(shè)置時間區(qū)間為東8
重新把time_zone改回東3
改回重新打開表,發(fā)現(xiàn)又回來了
不啟動SpringBoot,查詢數(shù)據(jù),還是老樣子
此時,添加一條數(shù)據(jù)。
往前推了2小時。
SpringBoot查詢
重啟SpringBoot,查出來就是庫中數(shù)據(jù)。
1、timeStamp類型存儲時間數(shù)據(jù),當(dāng)time_zone和serverTimeZone不統(tǒng)一情況,會話中,表中時間數(shù)據(jù),按照serverTimeZone設(shè)定
2、如果過程中,修改了time_zone變量,庫中數(shù)據(jù)直接變更。但是不影響當(dāng)前會話查詢,新增數(shù)據(jù),就會根據(jù)time_zone調(diào)整量,調(diào)整實際入庫。
3、SpringBoot重啟,其實就是新會話。情況同上。
4、時間字段類型為datetime,serverTimezone為GMT+8,jackson.time-zone為GMT+8,time_zone為東3
插入
庫中
查詢
time_zone從東3修改為東5
重新打開庫
不啟動SpringBoot
重啟SpringBoot,還是一樣。
修改serverTimeZone為GMT,其他不改動
查詢
總結(jié):如果是datetime類型,serverTimeZone說了算。
以上就是關(guān)于SpringBoot mysql數(shù)據(jù)庫時區(qū)問題的詳細內(nèi)容,更多關(guān)于SpringBoot mysql時區(qū)的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
