mybatis中日期类型大于、小于号转义符判断

在使用MyBatis查询涉及日期类型比较的SQL时,遇到由于日期与空字符串比较导致的异常。问题出现在MyBatis 3.3.0版本的一个bug,当尝试将日期类型与空字符串进行比较时,会抛出IllegalArgumentException。解决方案是避免在条件判断中包含日期与空字符串的比较,只保留非空判断即可修复此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、大小写转义符

在项目中查询时间段的sql语句(时间类型为datetime或date):
<if test="createdTimeStart != null">
            AND a.createdTime &gt;= #{createdTimeStart}
        </if>
        <if test="createdTimeEnd != null">
             AND a.createdTime &lt;= #{createdTimeEnd} 
        </if>
&lt;小于号  <      &gt; 大于号>

2、mybatis部分版本异常invalid comparison: java.util.Date and java.lang.String

严重: Servlet.service() for servlet [spring] in context with path [] threw exception [Request processing failed; nested exception is 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] with root cause

java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
at org.apache.ibatis.ognl.OgnlOps.compareWithConversion(OgnlOps.java:93)
at org.apache.ibatis.ognl.OgnlOps.isEqual(OgnlOps.java:143)
at org.apache.ibatis.ognl.OgnlOps.equal(OgnlOps.java:802)
at org.apache.ibatis.ognl.ASTNotEq.getValueBody(ASTNotEq.java:53)
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
at org.apache.ibatis.ognl.ASTAnd.getValueBody(ASTAnd.java:61)
at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
。。。。

表面上看是因为类型不符合, 但是想了想, Date类型对应MySQL的datetime, 以及mapper中jdbcType都没问题啊. 而且完全一样的东西在原工程中是完全正常的. 既然都是一样的代码, 那就找找俩工程有啥不一样的吧

首先是MySQL jar版本不同. 换成原工程中的版本也无效. 然后是mybatis jar版本不一样, 换成原工程中的版本问题就解决了!

原工程中配置的是mybatis-3.2.8, 而我测试工程中用的是mybatis-3.3.1.后来在网上找了一下才知道了原因:

    原来这是mybatis 3.3.0中对于时间参数进行比较时的一个bug. 如果拿传入的时间类型参数与空字符串''进行对比判断则会引发异常. 所以在上面的代码中去该该判断, 只保留非空判断就正常了

 

例如:在xxMapper.xml的把

<update id="updAccount" parameterType="com.isoftstone.ci.user.domain.Account" >
update usr_account set 
<trim  suffixOverrides="," >
<if test="sysAccountType != null  ">
sys_account_type=#{sysAccountType},
</if>
<if test="realNameAuthed != null  ">
real_name_authed=#{realNameAuthed},
</if>
<if test="lastLoginAt != null and lastLoginAt != ' ' ">
last_login_at=#{lastLoginAt}
</if>

</trim>
where id=#{id}
</update>

将代码换下面的代码(去掉时间跟空字符串的比较 lastLoginAt != ' ')

 

<update id="updAccount" parameterType="com.isoftstone.ci.user.domain.Account" >
update usr_account set 
<trim  suffixOverrides="," >
<if test="sysAccountType != null  ">
sys_account_type=#{sysAccountType},
</if>
<if test="realNameAuthed != null  ">
real_name_authed=#{realNameAuthed},
</if>
<if test="lastLoginAt != null ">
last_login_at=#{lastLoginAt}
</if>

</trim>
 where id=#{id}
</update>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值