MyBtis运⾏时异常之Date转换String 场景描述
存在Date类型的startTime和endTime,在查询时出错,其中l如下所⽰
<if test="startTime != null and startTime != ''">
AND ps.`update_time` <![CDATA[ >= ]]> #{startTime}
</if>
<if test="endTime != null and endTime != ''">
AND ps.`update_time` <![CDATA[ <= ]]> #{endTime}
</if>
异常信息
### Error querying databa. Cau: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
### Cau: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String]-[{}]
### Error querying databa. Cau: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
### Cau: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
batis.anslateExceptionIfPossible(MyBatisExceptionTranslator.java:79)
batis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:447)
at com.sun.proxy.$Proxy150.lectList(Unknown Source)
batis.spring.SqlSessionTemplate.lectList(SqlSessionTemplate.java:231)
at org.apache.ibatis.uteForMany(MapperMethod.java:137)
at org.apache.ibatis.ute(MapperMethod.java:75)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)
at com.sun.proxy.$Proxy167.listSyncData(Unknown Source)
flect.NativeMethodAccessorImpl.invoke0(Native Method)
flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at flect.Method.invoke(Method.java:498)
解决⽅案
将<if test="startTime != null and startTime != '' ">修改为<if test="startTime != null ">。这⾥就相当于date与string⽐较,故mybatis报错 invalid comparison: java.util.Date and java.lang.String。