起因经过结果
今天写代码,写一个用时间筛选的功能。
有一个近三月的按钮,前台传三个月以前的日期和今天的日期。后台使用String接收,接收到的格式为:
2018-09-04
使用
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
转换成Date,但是今天(假设今天是2018-09-04)十二点的数据就已经筛选不到了,所以对时间又进行了修改:
endDate=sdf.parse(end);
endDate.setTime(endDate.getTime()
+ (1000l*60l*60l*24l)-1000l);
// 开始时间不需要修改。
然后执行查询,部分查询语句下:
<if test="begin!=null ">
and t.create_time >=#{begin,jdbcType=DATE}
</if>
<if test="end!=null ">
and t.create_time <= #{end,jdbcType=DATE}
</if>
但是发现仍然查不出来今天十二点的数据。后来注意到,以前用的都是:
#{end,jdbcType=TIMESTAMP}
然后把sql改成:
<!-- 就是把DATA改成TIMESTAMP了 -->
<if test="begin!=null ">
and t.create_time >=#{begin,jdbcType=TIMESTAMP}
</if>
<if test="end!=null ">
and t.create_time <= #{end,jdbcType=TIMESTAMP}
</if>
执行查询,正常了。
结论
根据自己经历和查询,发现:
无论是传参:
and t.create_time >=#{end,jdbcType=TIMESTAMP}
还是设置返回值:
<result column="create_time" property="createTime"
jdbcType="TIMESTAMP"/>
jdbcType=”DATE” 都忽略了 时分秒, 无论是否有时分秒,都设置为了 00:00:00;
而jdbcType=”TIMESTAMP” 则 年月日时分秒全部设置,没有忽略。