Recently, you need to store a data stored in the HashMap key and value to the database. The concrete structure is roughly map<object, set<string>>. You need to save two fields in the database: multiple strings in the Id,set of an object in 1.key, obviously the string in ID and set is a 1-to-many relationship, and a nested loop is required. At first doubt whether MyBatis can achieve such a flexible SQL, after trying to prove that mybatis powerful. Specific practices: (Oracle database)
<insert id= "Savematchresult" > INSERT into Sp4_acl_so (acl_id, so_uuid__proc_def_id) <foreach
collection= "Acldevicemap.keys" index= "index" item= "item" separator= "UNION All" > <foreach collection= "acldevicemap[item" "Index=" Index_set "item=" set "separator=" UNION All "> SELECT #{item.aclid} , #{set} from DUAL </foreach> </foreach></insert>
Acldevicemap: The variable is labeled with @param ("Acldevicemap") in the interface
Using Acldevicemap.keys, you can take all the keys and traverse them. The inner Loop uses Acldevicemap[item] similar to Acldevicemap.get ("key") to take the value values corresponding to the current key. Because value is set, it also needs to be traversed. The results were saved successfully. Worship the mighty MyBatis again. Reference: http://btweibin.iteye.com/blog/1949534
Mybatis foreach nested traversal of key and value of map