在做mybatis的mapper.xml文件的时候,我们时常用到这样的情况:动态生成sql语句的查询条件,这个时候我们就可以用mybatis的foreach了
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.
public static void main(String[] args) {
List<List<Map<String, Object>>> mapsList = new ArrayList<>();
List<Map<String, Object>> maps = new ArrayList<>();
List<Map<String, Object>> maps1 = new ArrayList<>();
Map<String, Object> map = new HashMap<>();
map.put("fydm","3212");
map.put("ajbs","321200000006229");
maps.add(map);
Map<String, Object> map1 = new HashMap<>();
map1.put("fydm","3212");
map1.put("ajbs","321200000006229");
maps.add(map1);
List<Map<String, Object>> maps2 = new ArrayList<>();
Map<String, Object> map2 = new HashMap<>();
map2.put("fydm","3212");
map2.put("ajbs","321200000006229");
maps1.add(map2);
Map<String, Object> map3 = new HashMap<>();
map3.put("fydm","3212");
map3.put("ajbs","321200000006229");
maps1.add(map3);
mapsList.add(maps);
mapsList.add(maps1);
for (List<Map<String, Object>> mapList : mapsList) {
for (Map<String, Object> stringObjectMap : mapList) {
System.out.println(stringObjectMap.get("fydm")+"----"+ stringObjectMap.get("ajbs"));
}
}
baseMapper.insertInfoDocumentsBatch(mapsList);
}
<insert id="insertInfoDocumentsBatch" parameterType="java.util.List">
insert ignore hlwft_documents
(
document_name,
documents_number
)
values
<!-- mapsList: 也就是这个
List<List<Map<String, Object>>> mapsList = new ArrayList<>();
-->
<!-- mapListData: 也就是这个
List<Map<String, Object>> maps = new ArrayList<>();
List<Map<String, Object>> maps1 = new ArrayList<>();
-->
<!-- map: 也就是这个
Map<String, Object> map = new HashMap<>();
-->
<!-- separator=",": 这个重要是在每次循环的最后加上,-->
<!-- close=",": 这个是在所有都循环的最后加上,-->
<foreach item="mapListData" collection="mapsList" separator="," index="index1">
<foreach item="map" collection="mapListData" separator="," index="index2">
(
#{map.fydm},
#{map.ajbs}
)
</foreach>
</foreach>
<!-- 和Java for循环一致
for (List<Map<String, Object>> mapListData: mapsList) {
for (Map<String, Object> map : mapListData) {
System.out.println(map.get("fydm")+"----"+ map.get("ajbs"));
}
}
-->
</insert>