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表示值对象。

相关推荐
MegaDataFlowers11 小时前
MybatisPlus入门案例
mybatis
cheems952713 小时前
[Mybatis] #{ } 与 ${ } 的底层博弈与工程实践
mybatis
2601_949817721 天前
Spring Boot3.3.X整合Mybatis-Plus
spring boot·后端·mybatis
LaLaLa_OvO1 天前
mybatis 引用静态常量
java·mybatis
yaodong5181 天前
Spring 中使用Mybatis,超详细
spring·tomcat·mybatis
2601_949815332 天前
Spring Boot中集成MyBatis操作数据库详细教程
数据库·spring boot·mybatis
星晨雪海2 天前
若依框架原有页面功能进行了点位管理改造之列表查询(4)
数据库·sql·mybatis
mldlds2 天前
Spring Boot 集成 MyBatis 全面讲解
spring boot·后端·mybatis
那个失眠的夜2 天前
Spring整合Mybatis实现用户的CRUD
java·spring·mybatis
zjjsctcdl2 天前
Spring Boot 整合 MyBatis 与 PostgreSQL 实战指南
spring boot·postgresql·mybatis