亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

mybatis foreach 循環(huán) list(map)實(shí)例

 更新時(shí)間:2022年03月23日 08:57:24   作者:zhouixi  
這篇文章主要介紹了mybatis foreach 循環(huán) list(map)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

foreach 循環(huán) list(map)

直接上代碼:

整體需求就是

1.分頁(yè)對(duì)象里面有map map里面又有數(shù)組對(duì)象

2.分頁(yè)對(duì)象里面有l(wèi)ist list里面有map map里面有數(shù)組對(duì)象。

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(); 實(shí)體分頁(yè)對(duì)象(包括其他頁(yè)面屬性)		  
	maps.put("str", str); ? 批量查詢的ID		  
	page.setMaps(maps); ? ? maps對(duì)象保存在分頁(yè)屬性中		  
	List<Map> mapTest = userService.mapTest(page);		  
	System.out.println(mapTest);

需求。請(qǐng)求前臺(tái)頁(yè)面的時(shí)候 需要傳多個(gè)訂單號(hào)比如1,2

然而其他參數(shù)也要有。就要用到分頁(yè)實(shí)體 跟map結(jié)合 分頁(yè)實(shí)體保存其他屬性。map保存要循環(huán)的ID 或是訂單號(hào)

mybatis.foreach循環(huán)如下

這里只做ID或是訂單ID的演示,普通屬性#{id}就行。

取page.maps.str(str是一個(gè)數(shù)組)

在collection 這里面直接寫    入?yún)?maps

如果入?yún)⑹荓IST

稍微改一下即可

源數(shù)據(jù)

  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我要先循環(huán)list然后拿到list里面的map.str 即可。

使用foreach處理list中的map

參數(shù)的數(shù)據(jù)結(jié)構(gòu)是一個(gè)ArrayList<Map<String, Integer>>,需要以String,Integer為條件批量更新數(shù)據(jù)庫(kù)的數(shù)據(jù).

將參數(shù)封裝到叫做JsonData的qv中,JsonData的關(guān)鍵代碼是

? ? 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;
? ? }

Mapper中的方法是

updateXxxx(JsonData jsonData);

Mapper.xml的sql是

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

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論