mysql遍历map中的数组_19、mybatis学习——mybatis的动态sql之<foreach>遍历传入的数组,集合和map...

foreach元素的属性主要有item,index,collection,open,separator,close。

item:集合中元素迭代时的别名,该参数为必选。

index:在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选

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

separator:元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。

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

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"

在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:

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

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

如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.

举例一(根据id批量查询):

Student.java

36cf549e31b6b6bcaa83f826fe295cba.png

StudentMapper接口中定义方法

ab67f7b5c7b13753cde9fcb3fe186450.png

StudentMapper的配置文件配置

select * from student where id in

#{id}

测试方法

//测试通过遍历参数ids

@Testpublic void testGetStuByForeachId() throwsIOException {

String resource= "mybatis-config.xml";

InputStream inputStream=Resources.getResourceAsStream(resource);

SqlSessionFactory sqlSessionFactory= newSqlSessionFactoryBuilder().build(inputStream);

SqlSession sqlSession=sqlSessionFactory.openSession();

StudentMapper studentMapper= sqlSession.getMapper(StudentMapper.class);

List ids = new ArrayList();

ids.add(1);

ids.add(2);

ids.add(4);

List stus =studentMapper.getStuByForeachId(ids);

System.out.println(stus);

sqlSession.close();

}

测试结果

49ffbfe0958e37f8e07b32a3712ac14a.png

举例二(批量插入数据)

StudentMapper接口中定义方法

cf87e39ce82380f854ad6a4ccdae62e0.png

StudentMapper配置文件进行配置

insert into student(name,c_id) values(#{stu.name},#{stu.college.id})

测试方法:

//测试通过批量插入数据

@Testpublic void testInsertStusByForeach() throwsIOException {

String resource= "mybatis-config.xml";

InputStream inputStream=Resources.getResourceAsStream(resource);

SqlSessionFactory sqlSessionFactory= newSqlSessionFactoryBuilder().build(inputStream);

SqlSession sqlSession=sqlSessionFactory.openSession();

StudentMapper studentMapper= sqlSession.getMapper(StudentMapper.class);

List stus = new ArrayList<>();

stus.add(new Student(null, "小张", new College(1)));

stus.add(new Student(null, "小李", new College(2)));

studentMapper.insertStusByForeach(stus);

sqlSession.commit();

sqlSession.close();

}

测试结果

8c8c481b46daa995c800499be2421ddb.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值