Java Mybatis foreach nested foreach List<list<Object>>
When making mybatis files, we often use this situation:
Dynamically generate query conditions for SQL statements, and at this time we can use mybatis foreach
Properties of foreach element
Mainly include item, index, collection, open, separator, close.
-
item
: The alias when iterating elements in the collection, this parameter is required. -
index
: In list and array, index is the element's sequence number, in map, index is the element's key, this parameter is optional -
open
: The start symbol of the foreach code, generally used together (and close=")". Commonly used in in() and values(). This parameter is optional -
separator
: The separator between elements. For example, when in(), separator="," will be automatically separated by "," in the middle of the element to avoid manually typing commas to cause SQL errors, such as in(1,2,). This parameter is optional. -
close
: The close symbol of the foreach code, usually used together with open="(". It is commonly used when in() and values(). This parameter is optional. -
collection
: To be a foreach object, when used as an entry parameter, the List object defaults to use "list" instead of keys, the array object has "array" instead of keys, and the Map object does not have a default key. Of course, when using the entry parameter, you can use @Param("keyName") to set the key. After setting the keyName, list and array will be invalid.
In addition to the case of parameter entry, there is also a time when a certain field is used as a parameter object.
Give an example
If User has the property List ids. The incoming parameter is a User object, then this collection = "ids". If the User has attributes Ids ids; where Ids is an object, and Ids has an attribute List id; the incoming parameter is a User object, then collection = ""
When using foreach, the most critical and most error-prone thing is the collection attribute. This attribute must be specified, but the value of the attribute is different in different situations. There are three main situations:
- If the passed in is a single parameter and the parameter type is a List, the collection attribute value is list.
- If the passed in is a single parameter and the parameter type is an array, the property value of the collection is array.
- If there are multiple parameters passed in, we need to encapsulate them into a map. Of course, a single parameter can also be encapsulated into a map. In fact, if you pass in parameters, it will also be encapsulated into a map in MyBatis. The map's key is the parameter name, so at this time the collection attribute value is the key of the passed List or array object in the map encapsulated by itself.
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<>(); ("fydm","3212"); ("ajbs","321200000006229"); (map); Map<String, Object> map1 = new HashMap<>(); ("fydm","3212"); ("ajbs","321200000006229"); (map1); List<Map<String, Object>> maps2 = new ArrayList<>(); Map<String, Object> map2 = new HashMap<>(); ("fydm","3212"); ("ajbs","321200000006229"); (map2); Map<String, Object> map3 = new HashMap<>(); ("fydm","3212"); ("ajbs","321200000006229"); (map3); (maps); (maps1); for (List<Map<String, Object>> mapList : mapsList) { for (Map<String, Object> stringObjectMap : mapList) { (("fydm")+"----"+ ("ajbs")); } } (mapsList); }
<insert parameterType=""> insert ignore hlwft_documents ( document_name, documents_number ) values <!-- mapsList: That's it List<List<Map<String, Object>>> mapsList = new ArrayList<>(); --> <!-- mapListData: That's it List<Map<String, Object>> maps = new ArrayList<>(); List<Map<String, Object>> maps1 = new ArrayList<>(); --> <!-- map: That's it Map<String, Object> map = new HashMap<>(); --> <!-- separator=",": This is important to add at the end of each loop,--> <!-- close=",": This is added at the end of all loops,--> <foreach item="mapListData" collection="mapsList" separator="," index="index1"> <foreach item="map" collection="mapListData" separator="," index="index2"> ( #{}, #{} ) </foreach> </foreach> <!-- andJava forConsistent loop for (List<Map<String, Object>> mapListData: mapsList) { for (Map<String, Object> map : mapListData) { (("fydm")+"----"+ ("ajbs")); } } --> </insert>
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.