MyBatis:SQL语句中的foreach的详细介绍
2018-03-27 15:00
316 查看
foreach 也就是遍历迭代,在SQL中通常用在 in 这个关键词的后面
foreach元素的属性主要有 item,index,collection,open,separator,close。
分别代表:
item表示集合中每一个元素进行迭代时的别名,index用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符,close表示以什么结束
代码片段:<select id="selectByIds" resultType="com.wuuushao.pojo.User"> select * from user where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach></select>
而最为重要的就是collection属性了,既然是迭代就表示传入的参数是多个,这时候传入的参数就有以下几种可能:
1. list2.array3.map
下面分别代码演示
1. 传入的参数为list的时候
对应的Dao中的Mapper文件是:public List<User> selectByIds(List<Integer> ids);
xml文件代码片段:<select id="selectByIds" resultType="com.wuuushao.pojo.User"> select * from user where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach></select>
2. 传入的参数为Array的时候对应的Dao中的Mapper文件是:
public List<User> selectByIds(int[] ids);
xml文件代码片段:<select id="selectByIds" resultType="com.wuuushao.pojo.User"> select * from user where id in <foreach collection="array" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
3. 传入的参数为Map的时候
对应的Dao中的Mapper文件是:public List<User> selectByIds(Map<String, Object> params);
xml文件代码片段:<select id="selectByIds" resultType="com.wuuushao.pojo.User"> select * from user where id in <foreach collection="ids" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
map的时候需要注意的是:collection的值“ids”是存储在map中的key(比如:map.put("ids",ids));这个不是随便乱写的,尤其需要注意;
原文地址: https://blog.csdn.net/WUUUSHAO/article/details/53038506
foreach元素的属性主要有 item,index,collection,open,separator,close。
分别代表:
item表示集合中每一个元素进行迭代时的别名,index用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符,close表示以什么结束
代码片段:<select id="selectByIds" resultType="com.wuuushao.pojo.User"> select * from user where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach></select>
而最为重要的就是collection属性了,既然是迭代就表示传入的参数是多个,这时候传入的参数就有以下几种可能:
1. list2.array3.map
下面分别代码演示
1. 传入的参数为list的时候
对应的Dao中的Mapper文件是:public List<User> selectByIds(List<Integer> ids);
xml文件代码片段:<select id="selectByIds" resultType="com.wuuushao.pojo.User"> select * from user where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach></select>
2. 传入的参数为Array的时候对应的Dao中的Mapper文件是:
public List<User> selectByIds(int[] ids);
xml文件代码片段:<select id="selectByIds" resultType="com.wuuushao.pojo.User"> select * from user where id in <foreach collection="array" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
3. 传入的参数为Map的时候
对应的Dao中的Mapper文件是:public List<User> selectByIds(Map<String, Object> params);
xml文件代码片段:<select id="selectByIds" resultType="com.wuuushao.pojo.User"> select * from user where id in <foreach collection="ids" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
map的时候需要注意的是:collection的值“ids”是存储在map中的key(比如:map.put("ids",ids));这个不是随便乱写的,尤其需要注意;
原文地址: https://blog.csdn.net/WUUUSHAO/article/details/53038506
相关文章推荐
- MyBatis:SQL语句中的foreach的详细介绍
- MyBatis:SQL语句中的foreach标签的详细介绍
- MyBatis:SQL语句中的foreach的详细介绍
- PDO进行sql语句预处理和操作结果集详细介绍(二)
- MyBatis的动态sql语句详解,foreach等
- 以数据库字段分组显示数据的sql语句(详细介绍)
- 以数据库字段分组显示数据的sql语句(详细介绍)
- (转载)SQL语句中Group by语句的详细介绍
- mybatis学习详细动态sql语句示范
- SQL语句中Group by语句的详细介绍
- Mybatis 动态SQL语句foreach
- MyBatis中forEach实现in条件下的集合参数SQL语句
- mybatis动态sql之foreach语句
- 关于mybatis增删改查时条件为一个集合时如何利用foreach书写sql语句
- MyBatis动态sql详解(二) foreach语句详解
- Magento 1.5版本,导出order详细订单的SQL语句 、 取消订单事件 同时删除多个ID foreach
- MyBatis SQL语句 符号不兼容 大于号 小于号 (XML转义字符)
- MyBatis使用@SelectProvider拼接sql语句
- 关于mybatis 的SQL语句查询
- log4j打印mybatis的sql语句