MyBatis 动态元素 foreach元素

foreach元素

:
  foreach元素就相当于java中的for循环语句,它主要的目的还是做批量的操作。比如查询1000条数据中的一百条数据时,就需要使用foreach来提高效率。

foreach标签的属性:

collection:
  (1) 要做foreach循环的对象。作为入参(传入参数)时,List对象默认用"list"代替作为key键名,数组对象用"array"代替作为key键名,Map对象则是自己声明的key键名,没有默认的。
  
  (2)当然在作为入参时可以使用@Param(“keyName”)来设置键,设置keyName后,“list”,“array”默认键名将会失效。
  
  (3) 除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子:如果User有个List属性 l。入参是User对象,那么这个collection = “l”.如果User有对象属性 ids,ids有个List属性 id;入参是User对象,那么collection = “ids.id”
  
item:迭代中当前对象(value)的别名,该参数为必选。

index:如果传来的值是list或数组,index是元素的序号,下标。如果是map,index是当前键值队中的key(键名),该参数可选。

open:foreach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。该参数可选

separator:元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,并且会自动去除数据后的最后一个分割符,避免手动输入逗号导致sql错误,如in(1,2),最后一个 , 就被自动去除了。该参数可选。

close: foreach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。该参数可选。
 
  
   
   
:

实践一下

先上数据库:
在这里插入图片描述

第一种写法:

<select id="F" parameterType="list"
		resultType="com.itheima.po.Account">
		select * from account where id in
		<!--collection传入的组合数据名为list-->
		<!--item当前的数据值-->
		<foreach collection="list" item="id"  open="(" separator="," close=")" >
			#{id}
		</foreach>
	</select>

测试方法

// select foreach测试
	@Test
	public void foreachTest() throws Exception {
		SqlSession session = MyBatisUtils.getSqlSession();// 获取会话

		List<Integer> list = new ArrayList<Integer>();
		list.add(2);
		list.add(3);
		list.add(1);

		List<Account> users = session.selectList("com.itheima.mapper.CM1.F", list);
		session.close();// 关闭会话

		for (Account a : users) {
			System.out.println(a.toString());
		}
	}

来试着拼接下

基础的SQL语句:

select * from account where id in

执行foreach标签

1、遍历传入的list,每次数据间用“,”分隔,去除最后一个","

2,3,1

2、数据前后加上open和close

(2,3,1)

3、拼装原sql

select * from account where id in (2,3,1)

来看看测试结果
在这里插入图片描述

第二种写法,把list包装进了map:

<select id="F2" parameterType="map"
		resultType="com.itheima.po.Account">
		select * from account where id in
		<foreach collection="aa" item="id"  open="(" separator="," close=")" >
			#{id}
		</foreach>
	</select>

测试方法

// select foreach测试
	@Test
	public void foreachTest2() throws Exception {
		SqlSession session = MyBatisUtils.getSqlSession();// 获取会话

		List<Integer> list = new ArrayList<Integer>();
		list.add(2);
		list.add(4);
		list.add(1);
		Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();
		map.put("aa", list);
		List<Account> users = session.selectList("com.itheima.mapper.CM1.F2", map);
		session.close();// 关闭会话

		for (Account a : users) {
			System.out.println(a.toString());
		}
	}

来试着拼接下

基础的SQL语句:

select * from account where id in

执行foreach标签

1、遍历传入map中的键aa,每次数据间用“,”分隔,去除最后一个","

2,4,1

2、数据前后加上open和close

(2,4,1)

3、拼装原sql

select * from account where id in (2,4,1)

来看看测试结果
在这里插入图片描述
第三种写法,把整个Map当成键(这里用到_parameter):

_parameter:
  Mybatis内置的参数,代表整个参数,当传入foreach的值为单个参数时_parameter就是这个参数,当传入foreach的值为多个参数,参数会被封装为一个map,_parameter就是代表这个map.

<select id="F3" parameterType="map"
		resultType="com.itheima.po.Account">
		select * from account where id in
		<!-- item代表传入的键值队中的键值,value -->
		<foreach collection="_parameter" item="ixxxxd"   open="(" separator="," close=")" >
			#{ixxxxd}
		</foreach>
	</select>

测试方法

// select foreach测试
	@Test
	
	public void foreachTest3() throws Exception {
		SqlSession session = MyBatisUtils.getSqlSession();// 获取会话
		
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("aa", 1);
		map.put("", 4);
		map.put("a141sdf", 7);
		List<Account> users = session.selectList("com.itheima.mapper.CM1.F3", map);
		session.close();// 关闭会话

		for (Account a : users) {
			System.out.println(a.toString());
		}
	}

来试着拼接下

基础的SQL语句:

select * from account where id in

执行foreach标签

1、遍历传入的整个map,每次数据间用“,”分隔,去除最后一个","

1,4,7

2、数据前后加上open和close

(1,4,7)

3、拼装原sql

select * from account where id in (1,4,7)

看看结果:
在这里插入图片描述

能力有限,今天就测试到这里,其实foreach标签在修改方面也是很有效的,再见!

MyBatis动态SQL中的一些元素 if、set、trim、choose、foreach、bind

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值