效果如下:
說明:
時間范圍的查詢會存在以下問題:
1、如果單純采用年月日的形式會出現缺少最后一點的數據,比如要查詢2015-09-16到2015-09-17,那么2015-09-17 01:00:00的數據不會被查詢出來。無論是使用between and還是<=/>=的形式去實現都會有這樣的問題。
解決方法可以這樣做:
1、如果是以年月日的形式,那么可以采用動態拼接字符串的形式,最后得到2015-09-16 00:00:00到2015-09-17 23:59:59,如果要更精確可以往毫秒級別加。
2、采用加1天的形式,比如使用DATE_ADD去增加最后一天,最終得到2015-09-16到2015-09-18。
3、在前端時間控件上采用更精確的輸入,比如可以選擇年月日時分秒的級別,但是如果要精確到毫秒級別的,需要另做處理,還是使用拼接字符串的形式,或者采用增加函數去增加毫秒級別。
4、如果采用DATE_ADD去增加1天,那么會面臨一個問題,就是如果2017-09-18 00:00:00的數據就會被查出來,所以解決方法還是字符串拼接的形式會靠譜一些;或者如果用函數增加時間時,最好不要加滿到1天。(初步想法,沒實踐)
5、如果想要優雅的解決,最完美的方式應該是時間戳的形式,比如將時間轉成時間戳的形式去查詢。
MyBatis的時間段查詢方案:
說明:以下只是查詢的語句,沒涉及到精確到毫秒級別的查詢。
<!-- 查詢條件:創建開始時間 --> <if test="createdBegintime!=null and createdBegintime!=''"> AND CREATED >= #{createdBegintime} </if> <!-- 查詢條件:創建結束時間 --> <if test="createdEndtime!=null and createdEndtime!=''"> AND CREATED <= #{createdEndtime} </if>
<if test="date!=null"> <![CDATA[ AND CREATE_TIME >= CONCAT(#{date},' 00:00:00') AND CREATE_TIME <= CONCAT(#{finish},' 23:59:59') ]]> </if>
<![CDATA[ 這里面是sql語句. 大於號.小於號 ]]> 用這個把大於號.小於號包含起來
<select id="selectOrderListByPage" parameterType="com.xhh.webui.system.entity.Order" resultType="com.xhh.webui.system.entity.Order"> select * from `order` <where> <if test="begindate != null and begindate !=''"> createTime>#{begindate,jdbcType=TIMESTAMP} </if> <if test="enddate != null and enddate !=''"> and createTime<#{enddate,jdbcType=TIMESTAMP} </if> </where> <if test="sort != null and order != null"> <![CDATA[ order by ${sort} ${order} ]]> </if> LIMIT #{start},#{rows} </select>
參考:
http://www.cnblogs.com/zhangliang88/p/5479682.html
http://www.jquerycn.cn/a_15385
http://blog.csdn.net/luckyboyguo/article/details/50427086
http://blog.csdn.net/zl544434558/article/details/24428307?utm_source=tuicool&utm_medium=referral