mybatis exception invalid comparison: java.util.Date and java.lang.String

Keywords: Java Apache Attribute Mybatis

mapper files need to be recompiled and released after being changed in development. Because the project is large and time-consuming, it is convenient and fast. test Just write a little Java Engineering. dao, mapper and entity classes used in engineering are copied from engineering. data base It's the same. But there's a strange problem.


There is an attribute in the entity class

  1. private Date createTime;  

What is defined in the attribute database is

  1. create_time datetime  


Definition of this attribute mapping in mapper

  1. <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />  


Here are the corresponding Dao method SQL statements in mapper

  1. <select id="selectByCreateTime" resultMap="userMap">  
  2.   select * from user   
  3.   <where>  
  4.     <if test="createTime != null and createTime !='' " >  
  5.       date(create_time) = date(#{createTime,jdbcType=TIMESTAMP})  
  6.     </if>  
  7.   </where>  
  8. </select>  

Where the date() function is only used to intercept the date of time and seconds from the date of year, month, day, time and seconds to the date of year, month and day, which has no effect on the anomaly.

Create entities in test classes and assign values to their properties

  1. User user=new User();  
  2. user.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").parse("2016-01-18"));  

The query method dao.selectByCreateTime(user) is then executed with an error report

  1. Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:   
  2. ### Error querying database.  Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String  
  3. ### Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String  
  4.     at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)  
  5.     at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:122)  
  6.     at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:113)  
  7.     at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:122)  
  8.     at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:64)  
  9.     at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)  
  10.     at com.sun.proxy.$Proxy0.selectByCreateTime(Unknown Source)  
  11.     at mybatis.Test.buyerInfoTimeTest(Test.java:53)  
  12.     at mybatis.Test.main(Test.java:39)  
  13. Caused by: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String  
  14.     at org.apache.ibatis.ognl.OgnlOps.compareWithConversion(OgnlOps.java:92)  
  15.     at org.apache.ibatis.ognl.OgnlOps.isEqual(OgnlOps.java:142)  
  16.     at org.apache.ibatis.ognl.OgnlOps.equal(OgnlOps.java:794)  
  17.     at org.apache.ibatis.ognl.ASTNotEq.getValueBody(ASTNotEq.java:53)  
  18.     at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)  
  19.     at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)  
  20.     at org.apache.ibatis.ognl.ASTAnd.getValueBody(ASTAnd.java:61)  
  21.     at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)  
  22.     at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)  
  23.     at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:494)  
  24.     at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:458)  
  25.     at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:44)  
  26.     at org.apache.ibatis.scripting.xmltags.ExpressionEvaluator.evaluateBoolean(ExpressionEvaluator.java:32)  
  27.     at org.apache.ibatis.scripting.xmltags.IfSqlNode.apply(IfSqlNode.java:34)  
  28.     at org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply(MixedSqlNode.java:33)  
  29.     at org.apache.ibatis.scripting.xmltags.TrimSqlNode.apply(TrimSqlNode.java:55)  
  30.     at org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply(MixedSqlNode.java:33)  
  31.     at org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:41)  
  32.     at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:280)  
  33.     at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:80)  
  34.     at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:120)  
  35.     ... 7 more  


Looks like it's because the type doesn't match, but think about it, Date type corresponds to MySQL's datetime and jdbcType in mapper. And the exact same thing is completely normal in the original project. Since it's the same code, find out what's different between the two projects.


First is MySQL The jar version is different. Replacing the original version of the project is invalid. Then the mybatis jar version is different. Replacing the original version of the project will solve the problem! uuuuuuuuuu


mybatis-3.2.8 was configurated in the original project, and mybatis-3.3.0 was used in my test project. Later, I found out that this was a bug when comparing time parameters in mybatis 3.3.0. If we compare the incoming time type parameters with empty string', it will cause an exception. So we only keep the non-empty judgment in the code above. It's normal to break it.


  1. <if test="createTime != null and createTime !='' " >  
  2.   date(create_time) = date(#{createTime,jdbcType=TIMESTAMP})  
  3. </if>  

Change to

[html] view plain copy
  1. <if test="createTime != null">  
  2.   date(create_time) = date(#{createTime,jdbcType=TIMESTAMP})  
  3. </if> 


Origin: http://blog.csdn.net/wanghailong_qd/article/details/50673144


Posted by sasito on Sat, 22 Dec 2018 12:48:05 -0800