MyBatis中如何查詢某個(gè)時(shí)間段內(nèi)的數(shù)據(jù)
如何查詢某個(gè)時(shí)間段內(nèi)的數(shù)據(jù)
1、當(dāng)使用SQL語(yǔ)句查詢某個(gè)時(shí)間段的數(shù)據(jù)時(shí)
我們很自然的會(huì)想到使用between…and..來(lái)操作,但是如果使用between...and... 這個(gè)方法來(lái)查詢某個(gè)時(shí)間段的數(shù)據(jù)時(shí)是需要傳入兩個(gè)參數(shù)的,一個(gè)是起始時(shí)間,另一個(gè)是結(jié)束時(shí)間,且兩個(gè)參數(shù)必須要同時(shí)存在才能使用between...and...,而我們希望的是只傳入一個(gè)參數(shù)(起始時(shí)間或者結(jié)束時(shí)間)就能進(jìn)行查詢。
但是在使用MyBatis時(shí)如果只傳入一個(gè)參數(shù),則相應(yīng)的SQL語(yǔ)句是不會(huì)執(zhí)行的,所以不能使用between...and... 來(lái)進(jìn)行某個(gè)時(shí)間段數(shù)據(jù)的查詢。
2、可以使用" >= ... AND ... <=" 來(lái)解決
例如:
SELECT * FROM o_info WHERE create_date >= '2019-03-10 17:04:04' AND create_date ?<= '2019-03-15 17:04:28';
但是若要在MyBatis中使用,有一點(diǎn)需要注意:在MyBatis中使用">"和"<"時(shí)會(huì)提示"The content of elements must consist of well-formed character data or markup."這個(gè)錯(cuò)誤,原因是使用的">"和"<"不符合xml的書寫規(guī)范,所以需要把">"和"<"使用<![CDATA[ ]]>標(biāo)簽括起來(lái)才行。
使用<![CDATA[ ]]>標(biāo)簽后的代碼:
<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>
這樣就可以實(shí)現(xiàn)只選擇開始時(shí)間或者只選擇結(jié)束時(shí)間,同時(shí)選擇開始時(shí)間和結(jié)束時(shí)間來(lái)進(jìn)行查詢了。
3、如果查詢時(shí)出現(xiàn)異常提示
### Error querying database. Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String
是因?yàn)閭魅氲膮?shù)類型是日期類型,但是在<if test="beginDate != null and beginDate != ''">和<if test="endDate != null and endDate != ''">卻用這個(gè)日期類型的參數(shù)與空字符串進(jìn)行了比較,所以出現(xiàn)了程序運(yùn)行時(shí)異常,解決方法是將與空字符串比較的判斷去掉,只保留非空判斷即可。
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>
將日期時(shí)間,轉(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)換為日期時(shí)間
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>
日期等于某天
直接把大于、小于號(hào),改為等于號(hào)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java利用位運(yùn)算實(shí)現(xiàn)加減運(yùn)算詳解
這篇文章主要為大家介紹了如何使用位運(yùn)算來(lái)實(shí)現(xiàn)加減功能,也就是在整個(gè)運(yùn)算過(guò)程中不能出現(xiàn)加減符號(hào)。文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-12-12Java8利用Stream實(shí)現(xiàn)列表去重的方法詳解
這篇文章主要為大家介紹了Java利用Stream實(shí)現(xiàn)列表去重的幾種方法詳解,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下2022-04-04Java8與Scala中的Lambda表達(dá)式深入講解
這篇文章主要給大家介紹了關(guān)于Java8與Scala中Lambda表達(dá)式的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11解決springboot讀取application.properties中文亂碼問(wèn)題
初用properties,讀取java properties文件的時(shí)候如果value是中文,會(huì)出現(xiàn)亂碼的問(wèn)題,所以本文小編將給大家介紹如何解決springboot讀取application.properties中文亂碼問(wèn)題,需要的朋友可以參考下2023-11-11mybatis-plus的SafetyEncryptProcessor安全加密處理示例解析
這篇文章主要為大家介紹了mybatis-plus的SafetyEncryptProcessor安全加密處理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08ElasticSearch創(chuàng)建后索引修改數(shù)據(jù)類型方法步驟
Elasticsearch存儲(chǔ)數(shù)據(jù)之前需要先創(chuàng)建索引,類似于結(jié)構(gòu)型數(shù)據(jù)庫(kù)建庫(kù)建表,創(chuàng)建索引時(shí)定義了每個(gè)字段的索引方式和數(shù)據(jù)類型,這篇文章主要給大家介紹了關(guān)于ElasticSearch創(chuàng)建后索引修改數(shù)據(jù)類型的方法步驟,需要的朋友可以參考下2023-09-09解決IDEA maven 項(xiàng)目修改代碼不生效,mvn clean、install后才生效
這篇文章主要介紹了解決IDEA maven 項(xiàng)目修改代碼不生效,mvn clean、install后才生效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09