Mybatis foreach 使用與理解(支持Array,List,HashMap及相互嵌套)

Mjing888 8年前發布 | 39K 次閱讀 MyBatis3 持久層框架

來自: http://my.oschina.net/xiaohelong/blog/608035


Mybatis foreach 使用與理解(支持List<Hashmap<String,String>>)

</pre>

<foreach collection="param3.field" item="field" separator="," open="(" close=")">

        </foreach></pre> 對原始語句的理解: 

</div>

separator是每一條記錄輸出后面加上這個(最后一條不加)
open,close是將整個foreach的輸出進行概括。
collection是列表或者map等源數據集合
item 是元素具體單個

使用說明
參數傳遞是使用Entity的,所以的參數都是從entity讀取屬性,pageJoin實體內嵌的分頁實體,在這里定義了一個
List<HashMap<string,string>> filter;(注意要把getter,setter生成好,因為是private的,否則就會出錯,這一點容易忽略)
使用實例:
<foreach item="item" index="index" collection="pageJoin.filter">                    
                       <choose>
                           <when test="item.type=='field'">
                              and ${item.field}=#{item.val1}
                           </when>
                           <when test="item.type=='range'">
                              and ${item.field} between #{item.val1} and #{item.val2} 
                           </when>
                           <otherwise>
                             <!-- donothing -->
                           </otherwise>
                        </choose>
</foreach>
 本文由用戶 Mjing888 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!