Mybatis 多條件動態查詢

2021-07-16 08:44:51 字數 1132 閱讀 3322

例:簡單的乙個查詢

select * from student_tbl st  

where st.student_name like concat(concat('%', #),'%')    

但是此時如果studentname是null或空字串,此語句很可能報錯或查詢結果為空。此時我們使用if動態sql語句先進行判斷,如果值為null或等於空字串,我們就不進行此條件的判斷。

修改為:

select * from student_tbl st  

where st.student_name like concat(concat('%', #),'%')    

完整版:  

y" parametertype="studententity" resultmap="studentresultmap">  

select * from student_tbl st  

st.student_name like concat(concat('%', #),'%')    

and st.student_*** = #    

and st.student_birthday = #    

and st.class_id = #    

當if標籤較多時,這樣的組合可能會導致錯誤。

例如:  

select * from student_tbl st  

where  

st.student_name like concat(concat('%', #),'%')    

and st.student_*** = #    

中,引數studentname為null或』』,則或導致此sql組合成「where and」之類的關鍵字多餘的錯誤sql。

這時可以使用where動態語句來解決。這個「where」標籤會知道如果它包含的標籤中有返回值的話,它就插入乙個『where』。此外,如果標籤返回的內容是以and 或or 開頭的,則它會剔除掉。

select * from student_tbl st  

st.student_name like concat(concat('%', #),'%')  

and st.student_*** = #  

MS SQL之多條件動態查詢

在網上看到乙個sql server的多條件查詢,感覺很精練,並結合實際的專案,做了乙個帶分頁的儲存過程 create procedure sgetrtableheadp mc 獲取簡歷數目並建立臨時表分頁,帶多條件引數查詢 status varchar 50 city varchar 100 高階查...

多條件動態LINQ 組合查詢

本文章 參考 以往我們都是通過判斷的方式來拼接查詢的sql字串,但是現在我們面對是強型別的linq查詢,是否可以很方便的進行類似查詢。eg string userid string.empty userid e351d301 f64b 412c b9ef 573f41235af2 string us...

多條件查詢

思路 1.獲取引數值 2.生成查詢條件 3.獲取查詢結果 4.繫結查詢選項 呼叫geturlhtml方法生成查詢url 例如 var y2013 pnvshihufu qbeijing sxuhuiqu 得到url變數值 protected string geturlval string name ...