<select resultType="Blog">
select * from t_blog where title like "%"#{title}"%" and id in
<foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
上述collection的值为ids,是传入的参数Map的key,对应的Mapper代码:
public List<Blog> dynamicForeach3Test(Map<String, Object> params);
对应测试代码:
@Test
public void dynamicForeach3Test() {
SqlSession session = Util.getSqlSessionFactory().openSession();
BlogMapper blogMapper = session.getMapper(BlogMapper.class);
final List<Integer> ids = new ArrayList<Integer>();
ids.add(1);
ids.add(2);
ids.add(3);
ids.add(6);
Map<String, Object> params = new HashMap<String, Object>();
params.put("ids", ids);
params.put("title", "中国");
List<Blog> blogs = blogMapper.dynamicForeach3Test(params);
for (Blog blog : blogs)
System.out.println(blog);
session.close();
}
<4>嵌套foreach的使用:
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 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。
《项目实践》
@Override
public Container<Map<String,Object>> findAuditListInPage(
Map<String, Object> params) {
//1、参数组装
PageModel pageMode = new PageModel();
try {
if(params.get("page")!=null){
pageMode.setPage(Integer.parseInt(params.get("page").toString()));
}
if(params.get("rows")!=null){
pageMode.setRows(Integer.parseInt(params.get("rows").toString()));
}
} catch (Exception e) {
Assert.customException(RestApiError.COMMON_ARGUMENT_NOTVALID);
}
//分页条件组装
pageMode.putParam(params);
if(params.get("startCreateTime") !=null){
Date parse = DateUtil.parse(params.get("startCreateTime").toString(), DateUtil.yyyyMMddHHmmss);
params.put("startCreateTime",parse);
}
if(params.get("endCreateTime") !=null){
Date parse = DateUtil.parse(params.get("endCreateTime").toString(), DateUtil.yyyyMMddHHmmss);
params.put("endCreateTime",parse);
}
if(params.get("type") !=null){ //type可以多选
String typeString = params.get("type").toString();
String typeArray [] = typeString.split(",");
params.put("type", typeArray);
}
if(params.get("state") !=null){ //state可以多选
String stateString = params.get("state").toString();
if(stateString.equals(DictConstants.APPLICATION_STATE.AUDITING)
||stateString.equals(DictConstants.APPLICATION_STATE.WAITING_AUDIT)){
stateString = "waitingAudit,auditing";
}
String stateArray [] = stateString.split(",");
params.put("state", stateArray);
}