Mybatis date retrieval format error

Insert image description here

Problem recurrence

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

problem analysis

It seems that the problem is because when querying, trying to compare the date (start_time) in the database with the string (startTime), resulting in "java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String" mistake.

——————原Mybatis语句
 <if test="startTime != null and startTime != '' ">and chat.start_time = #{startTime}</if>
——————修复后Mybatis语句
   <if test="startTime != null "> and DATE_FORMAT(chat.start_time, '%Y-%m-%d')=DATE_FORMAT( #{startTime}, '%Y-%m-%d')</if>

Problem reflection

The problem encountered was because MyBatis tried to compare the date ( chat.start_time) in the database with the string ( ), which resulted in the "java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String" error. You successfully solved the problem startTimeby startTimecomparing the parameter directly to the date in the database and using the function to format them both into the same date string. DATE_FORMATSuch a modification works because it ensures that both have the same format when comparing, so type mismatch exceptions are no longer thrown. Now the query will compare chat.start_timeto startTimethe date string of the parameter, which is a viable approach provided the formats of the two match.

Guess you like

Origin blog.csdn.net/weixin_53742691/article/details/132756722