好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

mybatis 遍历foreach中or拼接的操作

我就废话不多说了,大家还是直接看看关键代码吧:

?

1

2

3

4

5

6

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中的动态sql需要and和or拼接条件判断的写法

通常在java后台或者前端js中多个条件组合判断中使用and或者or 如下

?

1

2

3

4

5

//使用&或者&& | 和||

if (a== 1 &b== 2 ){

//或者sql文中的and和or

select * from emp where empno= '7788' and deptno= '20'

但是动态sql呢?

 

那就需要()把每一个条件括起来在加and 或者or

?

1

2

3

4

5

6

//动态添加语句

<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>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/qq_33045443/article/details/99942323

查看更多关于mybatis 遍历foreach中or拼接的操作的详细内容...

  阅读:9次