我是靠谱客的博主 受伤鸭子,最近开发中收集的这篇文章主要介绍mysql遍历map中的数组_19、mybatis学习——mybatis的动态sql之遍历传入的数组,集合和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

最后

以上就是受伤鸭子为你收集整理的mysql遍历map中的数组_19、mybatis学习——mybatis的动态sql之遍历传入的数组,集合和map...的全部内容,希望文章能够帮你解决mysql遍历map中的数组_19、mybatis学习——mybatis的动态sql之遍历传入的数组,集合和map...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部