Mybatis 动态 SQL - foreach

动态SQL的另一个常见需求是需要迭代一个集合,通常用于构建IN条件。例如:

XML 复制代码
<select id="selectPostIn" resultType="domain.blog.Post">
  SELECT *
  FROM POST P
  <where>
    <foreach item="item" index="index" collection="list"
        open="ID in (" separator="," close=")" nullable="true">
          #{item}
    </foreach>
  </where>
</select>

​<foreach>​元素非常强大,它允许您指定一个集合,并声明在元素体内可用的item和index变量。它还允许您指定打开和关闭字符串,并添加一个分隔符放置在迭代之间。该元素非常智能,不会意外地附加额外的分隔符。

注意:您可以将任何可迭代对象(例如List、Set等)、任何Map对象或数组对象作为 ​<foreach>​元素的collection参数传递。当使用可迭代对象或数组时,index表示当前迭代的次数,而value item表示在此迭代中检索到的元素。当使用Map(或包含Map.Entry对象的Collection)时,index表示键对象,item表示值对象。

相关推荐
九皇叔叔2 小时前
【07】SpringBoot3 MybatisPlus 删除(Mapper)
java·开发语言·mybatis·mybatis plus
☀Mark_LY21 小时前
MyBatis-Flex入门以及多数据源配置
java·mybatis
人道领域1 天前
javaWeb从入门到进阶(SpringBoot基础案例2)
java·开发语言·mybatis
九皇叔叔1 天前
【06】SpringBoot3 MybatisPlus 修改(Mapper)
java·spring boot·mybatis·mybatisplus
mc_故事与你1 天前
前后端分离项目(springboot+vue+mybatis)-教学文档(SpringBoot3+Vue2)-4 (正在编写)
vue.js·spring boot·mybatis
秃头续命码农人1 天前
谈谈对Spring、Spring MVC、SpringBoot、SpringCloud,Mybatis框架的理解
java·spring boot·spring·mvc·maven·mybatis
bekote1 天前
mybatis-plus-generator模版
mybatis
cyforkk2 天前
MyBatis Plus 字段自动填充:生产级实现方案与原理分析
mybatis
九皇叔叔2 天前
【03】SpringBoot3 MybatisPlus BaseMapper 源码分析
java·开发语言·mybatis·mybatis plus
马猴烧酒.2 天前
【Mybatis出现bug】应为 <statement> 或 DELIMITER,得到 ‘id‘
java·bug·mybatis