1. 错误描述
在使用mysql时,如果数据库中的字段类型是timestamp,默认为0000-00-00,会发生异常:Value ‘0000-00-00 00:00:00’ can not be represented as java.sql.Timestamp.
2. 解决办法
给数据库的 jdbc.url 加上 zeroDateTimeBehavior 参数,如下:
jdbc.url=jdbc:mysql://127.0.0.1:3306/seckill?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=round
注意参数间不能有空格;否则报错 org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection
zeroDateTimeBehavior参数有两种配置:
- zeroDateTimeBehavior=round ,”0000-00-00“会默认转换为”0001-01-01 00:00:00”
- zeroDateTimeBehavior=convertToNull,“0000-00-00“会转换为null