简体   繁体   中英

MyBatis - Iterate over list which is value of HashMap in nested foreach loops

SELECT *
FROM mytable
<foreach item="item" index="index" collection="myhashmap.entrySet()" open=" WHERE (" separator=" OR ("> table.attribute= #{item.key} 
    <foreach item="item2" index="index" collection="#{item.value}" separator=" ," open=" AND table.attribute IN (" close="))">
       #{item2.value} 
         </foreach> 
 </foreach>     

The Iteration over the hashmap works, but the list (which is in item.value) doesn't work.

Map<Integer, List<Integer>> myhashmap = new HashMap<Integer, List<Integer>>();

{item.value.iterator()} doesn't work too.

Exception:
   org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error evaluating expression '#{item.value}'.  Return value ({[5800]=null}) was not iterable.

Where 5800 is a Integer-Value of the List

Any help is appreciated!

<foreach item="element" index="index" collection="mySet.entrySet()" open=" WHERE ((" separator=" OR ("> table.attribute1= #{element.key} 
    <foreach item="item" index="index" collection="element.value" separator=" ," open=" AND table.attribute2 IN (" close="))">
       #{item} 
         </foreach> 
 </foreach>  

This fixed it for me!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM