一、具体报错
Error attempting to get column 'last_login_time' from result set. Cause: java.sql.SQLException: Zero date value prohibited
翻译
尝试从结果集中获取列“last_login_time”时出错。原因:java.sql.SQLException:禁止零日期值
二、查看数据库设计
这里我给last_login_time的默认日期为 0000-00-00 00:00:00,报错零日期值,这就是问题所在,因为默认值给的全是0
三、问题原理及解决
查阅官网手册,如下内容
zeroDateTimeBehavior What should happen when the driver encounters
DATETIME values that are composed entirely of zeros (used by MySQL to
represent invalid dates)? Valid values are “exception”, “round” and
“convertToNull”. Default: exception Since version: 3.1.4
翻译
zeroDateTimeBehavior当驱动程序遇到
完全由零组成的DATETIME值(MySQL用于
表示无效日期)?有效值为“exception”、“round”和
“convertToNull”。默认值:“exception” 自版本:3.1.4
这里说当MySQL的Datetime的值全为0时默认是抛出异常, 也可以为round和convertToNull
再次查询文档,解释三个参数的含义
(1) exception (the default), which throws an SQLException with an SQLState
of S1009.
(2) convertToNull, which returns NULL instead of the date.
(3) round, which rounds the date to the nearest closest value which is
0001-01-01.
翻译
(1) exception(默认值),它将抛出带有SQLState的SQLException
状态S1009
(2) convertToNull,返回NULL而不是日期。
(3) round,将日期四舍五入到最接近的值,即
0001-01-01
解决
在MySQL连接Url参数后面加上zeroDateTimeBehavior 值可以为上面两种,或者改变MySQL字段的默认值
spring:
#数据库配置
datasource:
url: jdbc:mysql://127.0.0.1:3306/scan?zeroDateTimeBehavior=convertToNull