簡體   English   中英

MyBatis中的動態SQL(使用foreach)

[英]Dynamic SQL in MyBatis (using 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>

首先,請確保已在mybatis -config文件typeAliases標記中為java.util.HashMap設置了別名映射

<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

添加后為我解決了類似的錯誤

public SearchResultMap getSearchResultByParams(@Param("selectedGroups") List<String> selectedGroups) 

我的意思是@Param批注

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM