2015-06-10 64 views
2

我想通過列表迭代,但我正在逐漸例外動態SQL MyBatis的中(使用的foreach)

org.apache.ibatis.mapping.SqlMapperException: The expression 'list' evaluated to a null value. 

我的Java代碼:

public List<SearchVO> getSearchResultByParams(List<String> selectedGroups) { 
    Map map = new HashMap(1); 
    map.put("selectedGroups", selectedGroups); 
    return MyMapper.getSearchResultByParams(map); 
} 

MyMapper.xml:

<select id="getSearchResultByParams" parameterType="map" resultMap="SearchResultMap"> 
    SELECT * 
    FROM WORK 
    WHERE ID IN 
     <foreach item="selectedGroups" collection="list" open="(" separator="," close=")"> 
      #{selectedGroups} 
     </foreach> 
</select> 

回答

0

首先確保你已經爲mybatis-config文件中的java.util.HashMap設置了別名map 種類型別名標籤

<select id="getSearchResultByParams" parameterType="map" resultMap="SearchResultMap"> 
    SELECT * 
    FROM WORK 
    WHERE ID IN 
     <foreach collection="selectedGroups" item="item" index="index" open="(" separator="," close=")"> 
      #{item} 
     </foreach> 
</select> 

集合必須是關鍵你在地圖中列出

的醫生可能幫助你http://mybatis.github.io/mybatis-3/dynamic-sql.html

0

解決了我,我加

public SearchResultMap getSearchResultByParams(@Param("selectedGroups") List<String> selectedGroups) 
後類似的錯誤

我的意思是@Param註釋