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</span><Long> roleList=<span>new</span> ArrayList<Long><span>(); roleList.add(</span>10000000050086L<span>); roleList.add(</span>10000012180016L<span>); params.put(</span>"org"<span>, orgList); params.put(</span>"role"<span>, roleList); List</span><BpmDefUser> list=<span> bpmDefUserDao.getByMap(params); System.out.println(list.size()); }</span></pre>
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);}</span></pre>
xml代码如下:
<select id="getByMap" resultMap="BpmDefUser"><span><</span><span>foreach </span><span>collection</span><span>="relationMap"</span><span> index</span><span>="key"</span><span> item</span><span>="ent"</span><span> separator</span><span>="union"</span><span>></span><span> SELECT * FROM BPM_DEF_USER where RIGHT_TYPE=#{key} and OWNER_ID in </span><span><</span><span>foreach </span><span>collection</span><span>="ent"</span><span> item</span><span>="id"</span><span> separator</span><span>=","</span><span> open</span><span>="("</span><span> close</span><span>=")"</span><span>></span><span> #{id} </span><span></</span><span>foreach</span><span>></span> <span></</span><span>foreach</span><span>></span> <span></</span><span>select</span><span>></span></pre>
index 作为map 的key。item为map的值,这里使用了嵌套循环,嵌套循环使用ent。
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于