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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

enjoy嚣士   2021-09-10 我要评论
想了解Java timezone设置和mybatis连接数据库时区设置方式的相关内容吗,enjoy嚣士在本文为您仔细讲解timezone设置mybatis连接数据库时区设置的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java,timezone设置,mybatis连接数据库,时区设置,下面大家一起来学习吧。

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

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

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们