foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合

foreach元素的属性主要有 item,index,collection,open,separator,close。

  • item表示集合中每一个元素进行迭代时的别名,
  • index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,
  • open表示该语句以什么开始,
  • separator表示在每次进行迭代之间以什么符号作为分隔 符,
  • close表示以什么结束。

在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下4种情况:

1、如果传入的是单参数且参数类型是一个List的时候,collection属性值为list

public List listTest(List ids);

<select id="listTest" parameterType="java.util.List" resultType="Blog">
select * from t_blog where id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>

2、如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array

public List listTest(int[] ids);

<select id="listTest" parameterType="java.util.ArrayList" resultType="Blog">
select * from t_blog where id in
<foreach collection="array" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>

3、如果传入的参数是多个的时候,我们就需要把它们封装成一个Map或一个对象Object

public List listTest(Map params);

<select id="listTest" parameterType="java.util.HashMap" resultType="Blog">
select * from t_blog where title like "%"#{title}"%" and id in
<foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
----------------------------------------------------------------------------------
public List listTest(Blog blog); <select id="listTest" parameterType="Blog" resultType="Blog">
select * from t_blog where title like "%"#{title}"%" and id in
<foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>

4、如果传入的参数类型是一个String的时候,collection属性值为str.split(',')

public List listTest(String ids); // 逗号间隔

<select id="listTest" parameterType="java.lang.String" resultType="Blog">
select * from t_blog where id in
<foreach collection="ids.split(',')" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>

Mybatis 中 foreach 的四种用法的更多相关文章

  1. javascript中this的四种用法

    javascript中this的四种用法 投稿:hebedich 字体:[增加 减小] 类型:转载 时间:2015-05-11我要评论 在javascript当中每一个function都是一个对象,所 ...

  2. C#中 this关键字 四种用法

    /// <summary> /// 主程序入口 /// </summary> /// <param name="args"></param ...

  3. c++中for的四种用法

    #include <algorithm> #include <vector> #include <iostream> using namespace std; in ...

  4. C#中this的 四种 用法

    C#中的this用法,相信大家应该有用过,但你用过几种?以下是个人总结的this几种用法,欢迎大家拍砖,废话少说,直接列出用法及相关代码. this用法1:限定被相似的名称隐藏的成员 /// < ...

  5. 【转】javascript中this的四种用法

    在函数执行时,this 总是指向调用该函数的对象.要判断 this 的指向,其实就是判断 this 所在的函数属于谁. 在<javaScript语言精粹>这本书中,把 this 出现的场景 ...

  6. c#中foreach的一种用法

    以下定义的MyClass类中的addnum方法使用了一个数组型参数b: public class MyClass { ; public void addnum(ref int sum, params ...

  7. js中this 的四种用法

    this 在函数执行时,this 总是指向调用该函数的对象.要判断 this 的指向,其实就是判断 this 所在的函数属于谁. 在<javaScript语言精粹>这本书中,把 this  ...

  8. VBA中Option的四种用法

    1.Option Explicit.当使用Option Explicit时,必须在模块中的所有过程声明每一个变量,否则会出现语法错误并不能被编译.这样做的好处是,它能消除程序中因为错拼变量名而导致程序 ...

  9. JS中this的四种用法

    1.在一般函数方法中使用 this 指代全局对象 2.作为对象方法调用,this 指代上级对象 3.作为构造函数调用,this 指代new 出的对象 4.apply 调用 ,apply方法作用是改变函 ...

  10. JS中 this 的四种用法

    1.在一般函数中使用 this 指全局对象 window function fn(){ this.x = 1 } fn(); //相当于window.fn() 2.作为对象方法使用 this 指该对象 ...

随机推荐

  1. CSS样式第四篇

    ​针对现在网站的图片过大问题,可以用相应的工具进行压缩,并且可对图片进行切割处理. 1.如果一个页面的图片过大,可以对其切割,代码<img src="1.jpg">&l ...

  2. 一种利用光电容积描记(PPG)信号和深度学习模型对高血压分类的新方法

    具体的软硬件实现点击 http://mcu-ai.com/ MCU-AI技术网页_MCU-AI 据世界心脏联合会统计,截至 2022 年,全球有 13 亿人被诊断患有高血压,每年约有 1000 万人死 ...

  3. IPv6 — 基于邻居发现协议的通信方式

    目录 文章目录 目录 前文列表 IPv6 的通信方式 NDP(Neighbor Discovery Protocol,邻居发现协议) IPv6 地址自动配置 无状态自动配置概述 前文列表 <IP ...

  4. C++ 初始化列表(Initialization List)

    请注意以下继承体系中各class的constructors写法: 1 class CPoint 2 { 3 public: 4 CPoint(float x=0.0) 5 :_x(x){} 6 7 f ...

  5. C#.Net筑基-类型系统②常见类型

    01.结构体类型Struct 结构体 struct 是一种用户自定义的值类型,常用于定义一些简单(轻量)的数据结构.对于一些局部使用的数据结构,优先使用结构体,效率要高很多. 可以有构造函数,也可以没 ...

  6. .NET Framework 4.7.2下 Hangfire 的集成

    参考资料: 开源的.NET定时任务组件Hangfire解析:https://www.cnblogs.com/pengze0902/p/6583119.html.Net Core 简单的Hangfire ...

  7. Django——基于Ajax的登录功能实现

    urlpatterns = [ path('admin/', admin.site.urls), path('login/',views.login), path('get_validCode_img ...

  8. protoc-gen-go: error:inconsistent package names: , prototest

    如果你已经安装proto ,以及go生成proto插件.但还是报这种错误,请看一下是否 protoc --go_out=./ *.proto 指令打错了

  9. H.264码流解析

    这一篇内容旨在对H.264码流中的一些概念做简单了解. 1.概念了解 VCL:Video Coding Layer视频编码层,它是H.264(AVC)编码中的核心,负责视频数据的编码工作.VCL层会应 ...

  10. Windows库链接报错

    问题回溯 今天拿到别人已经编译好的库,发现在链接的时候出现了报错 [9/9 12.7/sec] Linking CXX shared module bin\plugins\AsensingPlugin ...