mybatis 遍历foreach中or拼接的操作
时间:2021-06-11 08:11:36|栏目:JAVA代码|点击: 次
我就废话不多说了,大家还是直接看看关键代码吧:
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 如下
//使用&或者&& | 和|| if(a==1&b==2){ } //或者sql文中的and和or select * from emp where empno='7788' and deptno='20'
但是动态sql呢?
那就需要()把每一个条件括起来在加and 或者or
//动态添加语句 <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>


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




