Mybatis报错(四)| invalid comparison: java.util.Date and java.lang.String

文章已参与[新人创作礼]活动,一起开启掘金创作之路.

微信公众号:秀基宝。如有问题,请后台留言,反正我也不会听。

前言

最近写查询时候报了一个莫名其妙的报错,这是一个小问题

报错

2022-05-08 10:28:48,056 ERROR WebMvcConfig:127 - nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### 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
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### 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.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)
   at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
   at com.sun.proxy.$Proxy126.selectList(Unknown Source)
   at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230)
   at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137)
   at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)
   at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
   at com.sun.proxy.$Proxy139.getInterField(Unknown Source)
   at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
复制代码
  • 废话不多说,直接上sql
<if test="downTypeDTO.dateCloum != null and downTypeDTO.dateCloum != ''">
    <if test="downTypeDTO.startDate != null and downTypeDTO.endDate != null">
        and ${downTypeDTO.dateCloum} between #{downTypeDTO.startDate} and #{downTypeDTO.endDate}
    </if>
    <if test="downTypeDTO.year != null and downTypeDTO.year != ''">
        and ${downTypeDTO.dateCloum} = #{downTypeDTO.year}
    </if>
</if>
复制代码

我之前还没仔细看,我在想year哪里有问题,查询啊,就一个等于,怎么变字符串,第一步我把#改成$发现不行,后来突然发现year是不是不能用!=‘’来判断,就改了一下。发现ok了,但是后来又有问题

  • 改良后
<if test="downTypeDTO.dateCloum != null">
    <if test="downTypeDTO.startDate != null and downTypeDTO.endDate != null">
        and ${downTypeDTO.dateCloum} between #{downTypeDTO.startDate} and #{downTypeDTO.endDate}
    </if>
    <if test="downTypeDTO.year != null">
        and ${downTypeDTO.dateCloum} = ${downTypeDTO.year}
    </if>
</if>
复制代码

继续报错

 java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Jan 01 00:00:00 CST 2023
        GROUP BY project_id' at line 18
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Jan 01 00:00:00 CST 2023
复制代码

从这里看,问题还是在这个时间,但是这是一个新问题。那就是内容使他报错,最后我看其他年份查询代码,将其$再次改回#,发现没问题了

最后贴一张没问题图 image.png

本人开发的玩基金小工具

个人博客:
名称:纯洁的麦田
链接:[http://www.idearyou.cn/]
描述:争取哪一天做上架构师
公众号:纯洁的麦田

网址:[xiu.idearyou.cn]
谷歌插件搜:秀基宝
小程序:秀基宝
复制代码
复制代码
复制代码
复制代码

后语

如果本文对你哪怕有一丁点帮助,请帮忙点好看。你的好看是我坚持写作的动力。 另外,关注免费学习。

猜你喜欢

转载自juejin.im/post/7095191786892574751