简体   繁体   中英

How to get a specific Object from HashMap in MyBatis foreach?

How can I get a specific object in a MyBatis foreach loop? I have written the following code:

<select id="getResult" resultMap="myResultMap" parameterType="java.util.HashMap">

    SELECT a.myselectfield_1, a.myselectfield_2                      
    FROM   ${mySchema}.test_a a    
    WHERE  a.field_x = 007
     AND   a.test_b IN 
             <foreach item="item" index="index" collection="#{map.get('myItems')}" open="(" separator="," close=")">
                #{item}         
            </foreach>
    ORDER BY a.myselectfield_1

</select>

This is the Java HashMap, which is relevant for the SQL Statement:

HashMap<String, Object> myMap = new HashMap<String, Object>();
myMap.put("mySchema", mySchema);
myMap.put("myItems", myArrayList);

I get the following error message:

Error querying database. Cause: java.lang.NullPointerException: target is null for method get

How can I read a specific value from a Java HashMap in MyBatis foreach? I´m using Version 1.1.1

您不需要使用get方法来访问map元素,只需在foreach标记中编写collection =“ myItems”或collection =“ map.myItems”即可,具体取决于您定义映射器接口的方式(后者假设您使用了@Param(“ map”)注释)。

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