mybatis的xml中判断in集合

<if test="deviceIds != null and deviceIds.size>0">
    and device_id in
    <foreach  item="item" collection="deviceIds" index="index"  open="(" separator="," close=")">
        #{item}
    </foreach>
</if>
  • collection:集合名称
  • item:单个对象在后续语句中的名称
  • index:序号
  • open:这段代码以这个字符开头
  • close:这段代码以这个字符结尾
  • separator:使用对应的字符进行分割
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Spring Boot使用MyBatis进行持久化操作时,XML可以使用条件判断语句实现动态SQL的生成。常见的条件判断方式包括: 1. if语句:根据参数值判断是否需要生成SQL片段。示例代码如下: ```xml <select id="getUserByName" resultMap="UserResultMap"> select * from user <where> <if test="name != null and name != ''"> and name = #{name} </if> </where> </select> ``` 2. choose-when-otherwise语句:根据不同的条件选择不同的SQL片段。示例代码如下: ```xml <select id="getUserByCondition" resultMap="UserResultMap"> select * from user <where> <choose> <when test="id != null"> and id = #{id} </when> <when test="name != null and name != ''"> and name = #{name} </when> <otherwise> and age >= #{minAge} </otherwise> </choose> </where> </select> ``` 3. foreach语句:遍历参数集合,生成多个SQL片段。示例代码如下: ```xml <update id="batchUpdateUsers"> update user <set> <foreach collection="list" item="item" separator=","> <if test="item.name != null"> name = #{item.name} </if> <if test="item.age != null"> age = #{item.age} </if> </foreach> </set> where id in <foreach collection="list" item="item" separator="," open="(" close=")"> #{item.id} </foreach> </update> ``` 总的来说,在Spring Boot使用MyBatis进行持久化操作时,XML的条件判断语句非常灵活,可以根据不同的需求生成动态SQL,并且可以通过参数传递控制SQL的生成。需要注意的是,在使用条件判断语句时,应尽量保持SQL的简洁和可读性,避免出现复杂的嵌套判断

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leo的心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值