Java timezone设置和mybatis连接数据库时区设置方式

来自:网络
时间:2021-09-30
阅读:
目录
Java timezone设置和mybatis连接数据库时区设置JVM时区设置MybatisMybatis timezone问题解决方法

Java timezone设置和mybatis连接数据库时区设置

JVM时区设置

springboot工程运行时,需要指定时区,这样获取的时间才会和系统时间相同。以下介绍方法:

1.查看当前时区

centos7以前(不含centos7)

cat /etc/sysconfig/clock

Java timezone设置和mybatis连接数据库时区设置方式

centos7以后(包含centos7)

timedatectl

Java timezone设置和mybatis连接数据库时区设置方式

在启动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&amp;useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=GMT%2B8"/>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持免费资源网。

返回顶部
顶部