mybatis 遍歷foreach中or拼接的操作
我就廢話不多說了,大家還是直接看看關(guān)鍵代碼吧:
select id, name from t_keys where 1 = 1 <if test='keys != null'> <foreach collection='keys' item='key' open='AND (' close=')' separator='or' > name = #{key} </foreach> </if>
補充:mybatis xml中的動態(tài)sql需要and和or拼接條件判斷的寫法
通常在java后臺或者前端js中多個條件組合判斷中使用and或者or 如下
//使用&或者&& | 和||if(a==1&b==2){}//或者sql文中的and和orselect * from emp where empno=’7788’ and deptno=’20’但是動態(tài)sql呢?
那就需要()把每一個條件括起來在加and 或者or
//動態(tài)添加語句<if test='(buhinmt.buhinCd!= null and buhinmt.buhinCd != ’’) or(buhinmt.syohinCd!= null and buhinmt.syohinCd != ’’)'> AND BM.buhin_cd = SMBM.buhin_cd AND SMBM.syohin_cd = SM.syohin_cd AND SM.syurisaki_cd = SYUM.syurisaki_cd </if>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. 關(guān)鍵字:oracle_sid,server_name,網(wǎng)絡(luò)連接,數(shù)據(jù)庫啟動2. Oracle9i在線表格重定義來組織表格3. 用腳本和查詢主動監(jiān)視Oracle 9i性能4. Oracle數(shù)據(jù)庫在線表格重定義功能簡介5. MySQL Community Server 5.1.496. MySql導(dǎo)出后再導(dǎo)入數(shù)據(jù)時出錯問題7. ORACLE創(chuàng)建DBlink的過程及使用方法8. Oracle建表與創(chuàng)建序列詳細(xì)實例9. MySQL慢查詢以及解決方案詳解10. SQL Server中的數(shù)據(jù)類型詳解
