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 指该对象 ...
随机推荐
- 在Docker内部使用gdb调试器报错-Operation not permitted
在docker内部使用gdb调试时刻遇到了gdb如下报错信息: warning: Error disabling address space randomization: Operation not ...
- 圣诞快乐 - Splashtop 2020 年回顾及未来展望
我们很高兴 Splashtop 的远程访问解决方案能够在这个充满挑战的时期为全球的组织和教育机构提供帮助. 实际上,2020 年 Splashtop 的每日使用量增加了400%!不仅如此,我们还增 ...
- kubernetes 之网络(canal)
https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/ 所有节点重设 ...
- vue-element-admin 运行踩坑笔记
npm WARN deprecated svgo@1.3.2: This SVGO version is no longer supported. Upgrade to v2.x.x. npm E ...
- SpringMVC在处理Ajax请求后返回void导致前台Ajax回调函数不执行
问题简述 在一次SpringMVC项目中,前台通过Ajax请求,准备修改数据库中的数据.因为Ajax请求只是让后台更改数据,所以Controller方法根本不需要返回给前台任何数据,所以我们将Cont ...
- Hibernate双向关联导致Java对象转换为JSON字符串时死循环问题的分析与解决方案
引言: 本文描述了在SSH框架中,多个持久层对象相互引用,从而引发分页查询中,查询所得的持久化对象转换为JSON字符串报错的原因及解决方案 使用EasyUI框架的小伙伴们都知道,在使用datagrid ...
- Django模板templates
1.模板文件的路径配置 2.模板中变量替换 3.变量渲染之深度查询 4.内置过滤器 过滤器的语法: {{obj|过滤器名称:过滤器参数}} 内置过滤器: 过滤器例子: 5.注释 6.多行注释 7.if ...
- linux下 IPv6组播(C++)
Server #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <s ...
- FRDM-MCXN947开发板之RGB灯
一.背景 RGB LED:通过红.绿.蓝三种颜色组合发光的LED,可以理解由三个不同发光属性的LED组成,这个是LCD平板显示原理的基础,一个LED相当于屏幕上面的一个像素 FRDM-MCXN947集 ...
- centos os7 和redhat 7 安装yum源失败的解决办法
首先看我的报错 [Errno 14] curl#6 - "Could not resolve host: mirrors.aliyun.com; Unknown error" yu ...