mybatis map foreach遍歷


mybatis 遍歷map實例

map 數據如下 Map<String,List<Long>>.

 

測試代碼如下:

public void getByMap(){
        Map<String,List<Long>> params=new HashMap<String, List<Long>>();
        List<Long> orgList=new ArrayList<Long>();
        orgList.add(10000003840076L);
        orgList.add(10000003840080L);
        
        List<Long> roleList=new ArrayList<Long>();
        
        roleList.add(10000000050086L);
        roleList.add(10000012180016L);
        
        params.put("org", orgList);
        params.put("role", roleList);
        
        List<BpmDefUser> list= bpmDefUserDao.getByMap(params);
        System.out.println(list.size());
        
    }

 

dao代碼如下:

public List<BpmDefUser> getByMap(Map<String,List<Long>> map){
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("relationMap", map);
        return this.getBySqlKey("getByMap", params);
        
    }

xml代碼如下:

<select id="getByMap" resultMap="BpmDefUser">
        
            <foreach collection="relationMap" index="key"  item="ent" separator="union">
                SELECT *
                FROM BPM_DEF_USER
                where  RIGHT_TYPE=#{key}
                and OWNER_ID in 
                <foreach collection="ent"  item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            </foreach>
        
    </select>
index 作為map 的key。item為map的值,這里使用了嵌套循環,嵌套循環使用ent。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM