mybatis foreach循环遍历map中的key和value
2017-01-17 17:52
357 查看
需求:按学生的姓名name、性别sex(单选框)及可选课程名称courses(复选框)进行高级查询
思路:将这些查询信息存放在map中
方法:
上面这种写法是比较灵活的。其实像这个需求,也可以写的简单一些。
思路:将这些查询信息存放在map中
方法:
Map<String,List<String>> condition=new HashMap<String,List<String>>(); //姓名 if(name.trim()!=null&&name.trim().length()>0){ List<String> stuName=new ArrayList<String>(); stuName.add(name.trim()); condition.put("s_name",stuName); } //性别 if(sex!=null&&sex.length()>0){ List<String> stuSex=new ArrayList<String>(); stuSex.add(name.trim()); condition.put("s_sex",stuSex); } //课程 if(courses!=null&&courses.length()>0){ List<String> stuCourses=new ArrayList<String>(); String[] courseArr=stuCourses.split(","); for(int i=0;i<courseArr.length;i++){ stuCourses.add(courseArr[i]); } condition.put("s_course",stuCourses); }mybatis:
<select id="getStudentsByCondition" parameterType="java.util.Map" resultMap="BaseResultMap"> select .....where <if test="condition != null"> <foreach collection="condition.keys" index="index" item="item"> <if test="condition[item]!=null and condition[item]!=''"> <choose> <when test="item=='s_course'"> and ${item} in <foreach collection="condition[item]" item="course" open="(" separator="," close=")"> #{course} </foreach> </when> <otherwise> and ${item} like '%'+ <foreach collection="condition[item]" item="others">#{others}</foreach> +'%' </otherwise> </choose> </if> </foreach> </if> </select>总结:其实这种mybatis的配置看起来还是挺复杂的,具体情况还要具体分析。
上面这种写法是比较灵活的。其实像这个需求,也可以写的简单一些。
相关文章推荐
- Map 循环出key 和 value
- java 通过map的value返回其对应的key (遍历map)
- 高效率遍历Map以及在循环过程中移除 remove指定key
- java 遍历map 排序map的key和value
- Map.Entry是同时遍历key和value两个用的
- java 遍历map中的key,和value 值
- 定义一个Map对象,遍历并打印出各元素的key和value
- jsp中遍历map,通过key和code对比,将map的value显示到页面
- c++ map sort by value and sort by key(字典的遍历)
- 遍历取出Map集合key-value数据的4种方法
- json对象转map方法之一,通过遍历json对象的所有key,取出value放入map
- java 遍历Map 和 根据Map的值(value)取键(key)
- Map循环获取key+value
- Mybatis foreach嵌套遍历Map的key和value
- Mybatis foreach嵌套遍历Map的key和value
- 把jsonString(为json对象)转为map,循环得到key,value
- 编写一个类,在main方法中定义一个Map对象(采用泛型),加入若干个对象,然后遍历并打印出各元素的key和value。
- mybatis如何遍历Map的key和value
- 一个关于遍历文件夹下的文件内容,遍历Map的key和value的例子
- js循环map 获取所有的key和value的实现代码(json)