欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mybatis异常invalid comparison: java.util.Date and java.lang.String

程序员文章站 2022-05-03 23:47:03
调试代码时发现报错,报错信息为:org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error updating database. Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.St...

调试代码时发现报错,报错信息为:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
### Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
	at com.sun.proxy.$Proxy132.update(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:294)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:63)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
	at com.sun.proxy.$Proxy223.updateDepartment(Unknown Source)
	at com.jxdinfo.mes.quality.service.impl.SamplingFeedbackServiceImpl.submitResolveInfo(SamplingFeedbackServiceImpl.java:174)
	at com.jxdinfo.mes.quality.service.impl.SamplingFeedbackServiceImpl$$FastClassBySpringCGLIB$$4c6fc9ad.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	...

看报错信息是由于Date和String的“无效的比较”,定位到xml文件中对应mapper的sql语句,对于日期字段的处理中。但是好像没什么问题。

<if test="handlingDate != null and handlingDate != '' ">
    handlingDate = #{handlingDate},
</if>

查了查资料,原来是mybatis对于时间参数比较的问题,如果与空字符串进行比较会引发报错

处理方法就是把对空字符串的判断去掉,只保留非空判断就好了。

改为:

<if test="handlingDate != null ">
    handlingDate = #{handlingDate},
</if>

本文地址:https://blog.csdn.net/kangrushuai/article/details/109647888