mybatis多个foreach_mybatis foreach 属性及其三种使用情况

本文详细介绍了 MyBatis 中 `foreach` 指令的使用,包括 `collection`、`item`、`index`、`open`、`close` 和 `separator` 等属性,并通过实例展示了单参数为 array、List 和 Map 类型时的查询操作。同时,文章还提及了多参数情况下如何利用 Map 进行操作。
摘要由CSDN通过智能技术生成

foreach 属性介绍

foreach 用于迭代传入过来的参数。

它的属性介绍分别是

collection:表示传入过来的参数的数据类型。该参数为必选。要做 foreach 的对象,作为入参时,List 对象默认用 list 代替作为键,数组对象有 array 代替作为键,Map 对象没有默认的键。当然在作为入参时可以使用 @Param(“keyName”) 来设置键,设置 keyName 后,list,array 将会失效。 除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子:

如果 User 有属性 List ids。入参是 User 对象,那么这个 collection = “ids” 如果 User 有属性 Ids ids;其中 Ids 是个对象,Ids 有个属性 List id;入参是 User 对象,那么 collection = “ids.id”

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

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

如果传入的参数是多个的时候,我们就需要把它们封装成一个 Map 了,当然单参数也可以封装成 map。

item: 循环体中的具体对象。支持属性的点路径访问,如 item.age,item.info.details。具体说明:在 list 和数组中是其中的对象,在 map 中是 value,该参数为必选。(它是每一个元素进行迭代时的别名)

index:在 list 和数组中,index 是元素的序号;在 map 中,index 是元素的 key。

open:表示该语句以什么开始

close:表示该语句以什么结束

separator:表示在每次进行迭代之间以什么符号作为分隔符

介绍完属性之后,下面就进入实践。首先先来看一个简单到爆炸的表(表名:t_test_foreach)

单参数是 array 类型

测试类

// ids = {1,2,3}

public List testFindByArray(int[] ids) throws Exception {

SqlSession sqlSession = getSession().openSession();

userList = sqlSession.selectList(NameSpace + ".findByArray", ids);

System.out.println(userList.toString());

sqlSession.close();

return userList;

}

1

2

3

4

5

6

7

8

9

mapper.xml

-->

SELECT id,`name` FROM t_test_foreach WHERE id IN

#{ids}

1

2

3

4

5

6

7

8

9

10

11

12

输出结果

DEBUG - ==> Preparing: SELECT id,`name` FROM t_test_foreach WHERE id IN ( ? , ? , ? )

DEBUG - ==> Parameters: 1(Integer), 2(Integer), 3(Integer)

DEBUG - <== Total: 3

[User{name='n1', id='1'}, User{name='n2', id='2'}, User{name='n3', id='3'}]

1

2

3

4

单参数是 List 类型

测试类

// List 元素有 1,3,5

public List testFindByList(List ids) throws Exception {

SqlSession sqlSession = getSession().openSession();

userList = sqlSession.selectList(NameSpace + ".findByList", ids);

System.out.println(userList.toString());

sqlSession.close();

return userList;

}

1

2

3

4

5

6

7

8

9

mapper.xml

SELECT id,`name` FROM t_test_foreach WHERE id IN

#{ids}

1

2

3

4

5

6

7

输出结果

DEBUG - ==> Preparing: SELECT id,`name` FROM t_test_foreach WHERE id IN ( ? , ? , ? )

DEBUG - ==> Parameters: 1(Integer), 3(Integer), 5(Integer)

DEBUG - <== Total: 3

[User{name='n1', id='1'}, User{name='n3', id='3'}, User{name='n5', id='5'}]

1

2

3

4

单参数是 Map 类型

测试类

// Map 中的元素有 int[] ids = {2, 4};map.put("ids", ids);

public List testFindByMap(Map map) throws Exception {

SqlSession sqlSession = getSession().openSession();

System.out.println(map.toString());

List objects = sqlSession.selectList(NameSpace + ".findByMap", map);

System.out.println(objects.toString());

sqlSession.close();

return userList;

}

1

2

3

4

5

6

7

8

9

10

mapper.xml

SELECT id,`name` FROM t_test_foreach WHERE id IN

#{id}

1

2

3

4

5

6

7

8

输出结果

DEBUG - ==> Preparing: SELECT id,`name` FROM t_test_foreach WHERE id IN ( ? , ? )

DEBUG - ==> Parameters: 2(Integer), 4(Integer)

DEBUG - <== Total: 2

[User{name='n2', id='2'}, User{name='n4', id='4'}]

1

2

3

4

多参数

这种情况在传参数时,一定要改用 Map 方式

测试类

public void testUpdateByParams(int[] ids,String name) throws Exception {

SqlSession sqlSession = getSession().openSession();

Map map = new HashMap();

map.put("ids",ids); // ids = {1,2,4}

map.put("name",name);// name = "updated"

sqlSession.selectList(NameSpace + ".findByParams", map);

sqlSession.close();

}

1

2

3

4

5

6

7

8

9

10

mapper.xml

UPDATE t_test_foreach SET `name` = '#{name}' WHERE id IN

#{item}

1

2

3

4

5

6

7

输出结果

DEBUG - ==> Preparing: UPDATE t_test_foreach SET `name` = ? WHERE id IN ( ? , ? , ? )

DEBUG - ==> Parameters: updated(String), 1(Integer), 2(Integer), 4(Integer)

1

2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值