這篇文章主要為大家展示了“mybatis foreach循環list的示例分析”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“mybatis foreach循環list的示例分析”這篇文章吧。
直接上代碼:
1.分頁對象里面有map map里面又有數組對象
2.分頁對象里面有list list里面有map map里面有數組對象。
public class Page {
private Map maps;
private List lists;
public Map getMaps() {
return maps;
}
public void setMaps(Map maps) {
this.maps = maps;
}
public List getLists() {
return lists;
}
public void setLists(List lists) {
this.lists = lists;
}
}
String [] str = {"1,2"};
Page page = new Page(); 實體分頁對象(包括其他頁面屬性)
maps.put("str", str); 批量查詢的ID
page.setMaps(maps); maps對象保存在分頁屬性中
List<Map> mapTest = userService.mapTest(page);
System.out.println(mapTest);需求。請求前臺頁面的時候 需要傳多個訂單號比如1,2
然而其他參數也要有。就要用到分頁實體 跟map結合 分頁實體保存其他屬性。map保存要循環的ID 或是訂單號

這里只做ID或是訂單ID的演示,普通屬性#{id}就行。
取page.maps.str(str是一個數組)
在collection 這里面直接寫 入參.maps
如果入參是LIST
稍微改一下即可
源數據
maps.put("str", str);
list.add(maps);
List<Map> mapTest = userService.mapTest1(list);
System.out.println(mapTest);<foreach item="items" index="index" collection="list" open="(" separator="," close=")"> -->
<foreach item="item" index="index" collection="items.str" open="(" separator="," close=")" >
#{item}
</foreach>
</foreach>
原理就是 先告訴mybatis我要先循環list然后拿到list里面的map.str 即可。
參數的數據結構是一個ArrayList<Map<String, Integer>>,需要以String,Integer為條件批量更新數據庫的數據.
將參數封裝到叫做JsonData的qv中,JsonData的關鍵代碼是
private ArrayList<Map<String, Integer>> usersPlatforms;
public ArrayList<Map<String, Integer>> getUsersPlatforms() {
return usersPlatforms;
}
public void setUsersPlatforms(ArrayList<Map<String, Integer>> usersPlatforms) {
this.usersPlatforms = usersPlatforms;
}updateXxxx(JsonData jsonData);
<update id="updateXxxx" parameterType="JsonData">
UPDATE xxx SET `xx` = 10
<where>
<foreach collection="usersPlatforms" item="userPlatform" open="" close="" separator="OR">
<foreach collection="userPlatform.keys" item="key" open=" user_id = " close="" separator="">
#{key}
</foreach>
<foreach collection="userPlatform.values" item="value" open=" AND platform = " close="" separator="">
#{value}
</foreach>
</foreach>
</where>
</update>以上是“mybatis foreach循環list的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。