引用博客:https://blog.csdn.net/wanghailong_qd/article/details/50673144
今天开发时遇到如下异常:invalid comparison: java.util.Date and java.lang.String
实体类中的属性
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime;
mapper映射
<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
数据库中该字段类型
mapper文件中的SQL语句
<if test="vo.startCreateTime !=null and vo.startCreateTime !=''">
and o.create_time >= #{vo.startCreateTime}
通过大佬博客找到原因为mybatis 3.3.0中对于时间参数进行比较时的一个bug. 如果拿传入的时间类型参数与空字符串''进行对比判断则会引发异常. 所以在上面的代码中去该该判断, 只保留非空判断就正常了。
更改为
<if test="vo.startCreateTime !=null and vo.startCreateTime !=''">
and o.create_time >= #{vo.startCreateTime}
关于@JsonFormat注解
该注解加在实体类中的日期类型上,可以将返回的Json字符串中的时间戳转换为具体时间
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date updateTime;
有该注解时效果图
没有注解时效果图