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

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的把

扫描二维码关注公众号,回复: 5758855 查看本文章

<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>

猜你喜欢

转载自blog.csdn.net/heng_yan/article/details/86691523
今日推荐