MyBatis中如何查詢某個時間段內(nèi)的數(shù)據(jù)
如何查詢某個時間段內(nèi)的數(shù)據(jù)
1、當使用SQL語句查詢某個時間段的數(shù)據(jù)時
我們很自然的會想到使用between…and..來操作,但是如果使用between...and... 這個方法來查詢某個時間段的數(shù)據(jù)時是需要傳入兩個參數(shù)的,一個是起始時間,另一個是結(jié)束時間,且兩個參數(shù)必須要同時存在才能使用between...and...,而我們希望的是只傳入一個參數(shù)(起始時間或者結(jié)束時間)就能進行查詢。
但是在使用MyBatis時如果只傳入一個參數(shù),則相應(yīng)的SQL語句是不會執(zhí)行的,所以不能使用between...and... 來進行某個時間段數(shù)據(jù)的查詢。
2、可以使用" >= ... AND ... <=" 來解決
例如:
SELECT * FROM o_info WHERE create_date >= '2019-03-10 17:04:04' AND create_date ?<= '2019-03-15 17:04:28';
但是若要在MyBatis中使用,有一點需要注意:在MyBatis中使用">"和"<"時會提示"The content of elements must consist of well-formed character data or markup."這個錯誤,原因是使用的">"和"<"不符合xml的書寫規(guī)范,所以需要把">"和"<"使用<![CDATA[ ]]>標簽括起來才行。
使用<![CDATA[ ]]>標簽后的代碼:
<if test="beginDate != null and beginDate != ''">AND stock_bill.bill_date <![CDATA[>=]]> #{beginDate}</if> <if test="endDate != null and endDate != ''">AND stock_bill.bill_date <![CDATA[<=]]>#{endDate}</if>
這樣就可以實現(xiàn)只選擇開始時間或者只選擇結(jié)束時間,同時選擇開始時間和結(jié)束時間來進行查詢了。
3、如果查詢時出現(xiàn)異常提示
### Error querying database. Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
是因為傳入的參數(shù)類型是日期類型,但是在<if test="beginDate != null and beginDate != ''">和<if test="endDate != null and endDate != ''">卻用這個日期類型的參數(shù)與空字符串進行了比較,所以出現(xiàn)了程序運行時異常,解決方法是將與空字符串比較的判斷去掉,只保留非空判斷即可。
4、最終的代碼:
?<if test="beginDate != null">AND stock_bill.bill_date <![CDATA[>=]]> #{beginDate}</if> ?<if test="endDate != null">AND stock_bill.bill_date <![CDATA[<=]]>#{endDate}</if>
Mybatis查詢?nèi)掌诜秶?/h2>
將日期時間,轉(zhuǎn)換為字符串
select s.* from BIZ_ASSAY_ORDER_SAMPLE s LEFT JOIN BIZ_ASSAY_ORDER o on o.ID=s.ORDER_ID WHERE 1=1
<if test="status !=null and status !=''"> ? ? AND s.RECORD_STATUS=#{status} </if> <if test="sampleNo !=null and sampleNo !=''"> ? ? AND s.SAMPLE_NO LIKE '%'||#{sampleNo}||'%' </if> <if test="orderNo !=null and orderNo !=''"> ? ? AND o.ORDER_NO LIKE '%'||#{orderNo}||'%' </if> <if test="sampleBizModelId !=null and sampleBizModelId !=''"> ? ? AND s.SAMP_BIZ_MODE_ID=#{ sampleBizModelId } </if> <if test="statusFlag !=null and statusFlag !=''"> ? ? AND s.STATUS_FLAG=#{ statusFlag } </if> <if test="fromDate != null and fromDate !=''"> ? ? and to_char(o.ORDER_DATE,'yyyy-MM-dd') >= ?#{fromDate} </if> <if test="toDate != null and toDate !=''"> ? ? and to_char(o.ORDER_DATE,'yyyy-MM-dd') <= ?#{toDate} </if>
關(guān)鍵代碼
<if test="fromDate != null and fromDate !=''"> ? ? and to_char(o.ORDER_DATE,'yyyy-MM-dd') >= ?#{fromDate} </if> <if test="toDate != null and toDate !=''"> ? ? and to_char(o.ORDER_DATE,'yyyy-MM-dd') <= ?#{toDate} </if>
或者
將字符串,轉(zhuǎn)換為日期時間
SELECT <include refid="Base_Column_List"/> FROM BIZ_DAILY_PLAN dp LEFT JOIN DIC_TEST_OBJECT tb ON dp.TEST_OBJECT_ID=tb.ID LEFT JOIN DIC_SAMPLE_BIZ_MODEL bm ON dp.SAMP_BIZ_MODE_ID=bm.ID LEFT JOIN RES_LABORATORY l ON dp.LABORATORY_ID=l.ID WHERE 1=1 AND dp.RECORD_STATUS = ${@cn.com.hwasunsoft.lims.core.enums.RecordStatusEnum@VALID.getValue()}
<if test="dailyPlanStatus !=null and dailyPlanStatus !=''"> ? ? AND dp.CONFIRMATION_STATUS=#{dailyPlanStatus} </if> <if test="status !=null and status !=''"> ? ? AND dp.RECORD_STATUS=#{status} </if> <if test="labId !=null and labId !=''"> ? ? AND dp.LABORATORY_ID=#{labId} </if> <if test="fromDate !=null and fromDate !=''"> ? ? AND dp.PLAN_DATE >= TO_DATE(#{fromDate},'yyyy-mm-dd') </if> <if test="toDate !=null and toDate !=''"> ? ? AND TO_DATE(#{toDate},'yyyy-mm-dd') >= dp.PLAN_DATE </if>
關(guān)鍵代碼
<if test="fromDate !=null and fromDate !=''"> ? ? AND dp.PLAN_DATE >= TO_DATE(#{fromDate},'yyyy-mm-dd') </if> <if test="toDate !=null and toDate !=''"> ? ? AND TO_DATE(#{toDate},'yyyy-mm-dd') >= dp.PLAN_DATE </if>
日期等于某天
直接把大于、小于號,改為等于號
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java8利用Stream實現(xiàn)列表去重的方法詳解
這篇文章主要為大家介紹了Java利用Stream實現(xiàn)列表去重的幾種方法詳解,文中的示例代碼講解詳細,需要的小伙伴可以參考一下2022-04-04解決springboot讀取application.properties中文亂碼問題
初用properties,讀取java properties文件的時候如果value是中文,會出現(xiàn)亂碼的問題,所以本文小編將給大家介紹如何解決springboot讀取application.properties中文亂碼問題,需要的朋友可以參考下2023-11-11mybatis-plus的SafetyEncryptProcessor安全加密處理示例解析
這篇文章主要為大家介紹了mybatis-plus的SafetyEncryptProcessor安全加密處理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08ElasticSearch創(chuàng)建后索引修改數(shù)據(jù)類型方法步驟
Elasticsearch存儲數(shù)據(jù)之前需要先創(chuàng)建索引,類似于結(jié)構(gòu)型數(shù)據(jù)庫建庫建表,創(chuàng)建索引時定義了每個字段的索引方式和數(shù)據(jù)類型,這篇文章主要給大家介紹了關(guān)于ElasticSearch創(chuàng)建后索引修改數(shù)據(jù)類型的方法步驟,需要的朋友可以參考下2023-09-09解決IDEA maven 項目修改代碼不生效,mvn clean、install后才生效
這篇文章主要介紹了解決IDEA maven 項目修改代碼不生效,mvn clean、install后才生效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09