1. 程式人生 > >MyBatis jdbcType=DATE 和 jdbcType=TIMESTAMP 的區別

MyBatis jdbcType=DATE 和 jdbcType=TIMESTAMP 的區別

起因經過結果

今天寫程式碼,寫一個用時間篩選的功能。

有一個近三月的按鈕,前臺傳三個月以前的日期和今天的日期。後臺使用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 &gt;=#{begin,jdbcType=DATE}
</if>
<if test="end!=null ">
    and t.create_time &lt;= #{end,jdbcType=DATE}
</if>

但是發現仍然查不出來今天十二點的資料。後來注意到,以前用的都是:

#{end,jdbcType=TIMESTAMP
}

然後把sql改成:

<!-- 就是把DATA改成TIMESTAMP了  -->
<if test="begin!=null ">
    and t.create_time &gt;=#{begin,jdbcType=TIMESTAMP}
</if>
<if test="end!=null ">
    and t.create_time &lt;= #{end,jdbcType=TIMESTAMP}
</if>

執行查詢,正常了。

結論

根據自己經歷和查詢,發現:
無論是傳參:

and t.create_time &gt;=#{end
,jdbcType=TIMESTAMP}

還是設定返回值:

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

jdbcType=”DATE” 都忽略了 時分秒, 無論是否有時分秒,都設定為了 00:00:00;
而jdbcType=”TIMESTAMP” 則 年月日時分秒全部設定,沒有忽略。