Mybatis 中 foreach 的四种用法
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 的四种用法的更多相关文章
- javascript中this的四种用法
javascript中this的四种用法 投稿:hebedich 字体:[增加 减小] 类型:转载 时间:2015-05-11我要评论 在javascript当中每一个function都是一个对象,所 ...
- C#中 this关键字 四种用法
/// <summary> /// 主程序入口 /// </summary> /// <param name="args"></param ...
- c++中for的四种用法
#include <algorithm> #include <vector> #include <iostream> using namespace std; in ...
- C#中this的 四种 用法
C#中的this用法,相信大家应该有用过,但你用过几种?以下是个人总结的this几种用法,欢迎大家拍砖,废话少说,直接列出用法及相关代码. this用法1:限定被相似的名称隐藏的成员 /// < ...
- 【转】javascript中this的四种用法
在函数执行时,this 总是指向调用该函数的对象.要判断 this 的指向,其实就是判断 this 所在的函数属于谁. 在<javaScript语言精粹>这本书中,把 this 出现的场景 ...
- c#中foreach的一种用法
以下定义的MyClass类中的addnum方法使用了一个数组型参数b: public class MyClass { ; public void addnum(ref int sum, params ...
- js中this 的四种用法
this 在函数执行时,this 总是指向调用该函数的对象.要判断 this 的指向,其实就是判断 this 所在的函数属于谁. 在<javaScript语言精粹>这本书中,把 this ...
- VBA中Option的四种用法
1.Option Explicit.当使用Option Explicit时,必须在模块中的所有过程声明每一个变量,否则会出现语法错误并不能被编译.这样做的好处是,它能消除程序中因为错拼变量名而导致程序 ...
- JS中this的四种用法
1.在一般函数方法中使用 this 指代全局对象 2.作为对象方法调用,this 指代上级对象 3.作为构造函数调用,this 指代new 出的对象 4.apply 调用 ,apply方法作用是改变函 ...
- JS中 this 的四种用法
1.在一般函数中使用 this 指全局对象 window function fn(){ this.x = 1 } fn(); //相当于window.fn() 2.作为对象方法使用 this 指该对象 ...
随机推荐
- uniapp去除button的边框
button { border: none !important; } button::after { border: none !important; }
- 面向K-12学生的远程访问学校计算机实验室
为了应对新冠肺炎大流行,许多学校被迫采用远程学习和混合时间制度.在家学习的学生必须使用自己的个人设备或学校提供的设备(例如 Chromebook )来完成课堂作业. 尽管许多解决方案可以帮助学生和 ...
- 鸿蒙HarmonyOS实战-Stage模型(开发卡片页面)
一.开发卡片页面 HarmonyOS元服务卡片页面(Metaservice Card Page)是指在HarmonyOS系统中,用于展示元服务的页面界面.元服务是指一组提供特定功能或服务的组件,例如天 ...
- win10激活方法
slmgr /ipk W269N-WFGWX-YVC9B-4J6C9-T83GX slmgr /skms zh.us.to slmgr /ato
- 阿里巴巴 MySQL 数据库之建表规约(一)
建表规约 强制部分 [强制] 表达是与否概念的字段,必须使用 is_xxx 的方式命名,数据类型是 unsigned tinyint (1 表示是,0 表示否). 说明:任何字段如果为非负数,必须是 ...
- Abp vNext 框架 文章
http://www.vnfan.com/helinbin/tag/Abp%20vNext框架/
- 怎么使用Stable diffusion中的models
Stable diffusion中的models Stable diffusion model也可以叫做checkpoint model,是预先训练好的Stable diffusion权重,用于生成特 ...
- LeetCode 699. Falling Squares 掉落的方块 (Java)
题目: On an infinite number line (x-axis), we drop given squares in the order they are given. The i-th ...
- ubuntu server 网速测试
ubuntu server 网速测试 speedtest-cli是一个用于测试网络带宽的命令行工具,可以快速测量下载和上传速度.你可以按照以下步骤安装和使用它: 打开终端. 安装speedtest-c ...
- OAuth + Security - 错误收集
Could not decode JSON for additional information: BaseClientDetails 完整的错误输出如下: 2019-12-03 22:18:37.2 ...