mybatis 日期查询datetime
<select id="getHistoryDataByDate" parameterType="java.util.HashMap" resultType="java.util.HashMap"> select * from ${tableName} <where> <if test="dStart!=null"> <![CDATA[ and DATE_FORMAT(time, '%Y-%m-%d %H:%T:%s') >= DATE_FORMAT(#{dStart}, '%Y-%m-%d %H:%T:%s') ]]> </if> <if test="dEnd!=null"> <![CDATA[ and DATE_FORMAT(time, '%Y-%m-%d %H:%T:%s') <= DATE_FORMAT(#{dEnd} , '%Y-%m-%d %H:%T:%s') ]]> </if> </where> order by time desc </select>
注意使用if test 语句对传入的起止判断为如下会报错:java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
<if test="dStart!=null and dStart!=''"> <![CDATA[ and DATE_FORMAT(time, '%Y-%m-%d %H:%T:%s')>= DATE_FORMAT(#{dStart}, '%Y-%m-%d %H:%T:%s') ]]> </if> <if test="dEnd!=null and dEnd!=''"> <![CDATA[ and DATE_FORMAT(time, '%Y-%m-%d %H:%T:%s') <= DATE_FORMAT(#{dEnd}, '%Y-%m-%d %H:%T:%s') ]]> </if>
原文链接:https://blog.csdn.net/edison_03/article/details/77413099