MyBatis foreach 如何从 HashMap 中获取特定的对象?

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

如何在 MyBatis foreach 循环中获取特定对象?我写了下面的代码:

<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>

这是 Java HashMap,与 SQL 语句相关:

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

我收到以下错误消息:

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

如何从 MyBatis foreach 中的 Java HashMap 中读取特定值?我使用的是版本 1.1.1

你不需要使用get方法来访问地图元素,你可以在foreach标签中简单地写collection="myItems"或collection="map.myItems",取决于你的方式定义了您的映射器接口(后者假定您使用了 @Param("map") 注释)。

您应该在 foreach 中使用 $ 替换 #。

更多信息可以看:

Mybatis foreach collection is a list in a map-parameter

MyBatis Issue with IN Condition <foreach with List inside a Map