Mybatis foreach nested traversal of key and value of map

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.