Error querying 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.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
... 37 more
<if test="startTime!=null and startTime!=''">
and CREATE_TIME >=#{startTime}
</if>
<if test="endTime!=null and endTime!=''">
and CREATE_TIME <=#{endTime}
</if>
时间类型与空字符串进行比较会抛异常,所以判断时间类型是否为空时,只需要去除!=‘’就可以了
<if test="startTime!=null">
and CREATE_TIME >=#{startTime}
</if>
<if test="endTime!=null">
and CREATE_TIME <=#{endTime}
</if>
另外一种是需要设置pojo类中该属性为Stirng类型,同时数据库需要转换成Date类型,这里使用的oracle
<if test="startTime!=null and startTime!=''">
and CREATE_TIME >=to_date(#{startTime},'yyyy-mm-dd')
</if>
<if test="endTime!=null and endTime!=''">
and CREATE_TIME <=to_date(#{endTime},'yyyy-mm-dd')
</if>