title: JAVA | mybatis | Insert a multimap
body: <p>I have a use-case wherein i want to insert a multimap ( eg. Multimap ) into database using mybatis.</p>
  
  <p>i am not able to access key and value in mybatis using the following :
  (assumed that it would invoke the entrySet and iterate on it internally before inserting)</p>
  
  <pre><code>INSERT INTO table1 (integer1, integer2)
  VALUES (#{key} , #{value})
  </code></pre>
  
  <p>Any idea how this can be done ?
  I am assuming there should be a straightforward way, as this is a basic use case.</p>
  
  <p>--EDIT--</p>
  
  <p>Though i can create a wrapper object, set these values and send them to mybatis, i wanted to know if there is any other way to achieve the end result without creating additional objects, as this is the only place where i need them.</p>
  
  <p>Thanks for the help.</p>
  
  <p>---EDIT---
  Tested after incorporating the suggested solution.</p>
  
  <p>Code snippet for details:</p>
  
  <p>DAO layer:</p>
  
  <pre><code>Map&lt;String, Object&gt; params = new HashMap&lt;&gt;();
  params.put("entries", myMultimap.entries());
  </code></pre>
  
  <p>Mybatis sql:</p>
  
  <pre><code>INSERT INTO table1
      (integer1, integer2)
  VALUES
  &lt;foreach item="item" separator="," collection="entries"&gt;
     ( #{item.key} , #{item.value} )
  &lt;/foreach&gt;
  </code></pre>
  
  <p>--- EDIT ---</p>
  
  <p>Guys, with the above solution watch out for max 2000 characters in parameter length. If you face the issue, batching/bulk insert will be the way to proceed further.</p>