mybatis sql in 查询

 

 

 1 1.准备查询参数:
 2 
 3 List<String> list = new ArrayList<String>();
 4 list.add("x");
 5 list.add("y");
 6 list.add("z");
 7 
 8 String s = "A"
 9 
10 Date d = new Date();
11 
12 Map<String,Object> param = new HashMap<String,Object>();
13 
14 param.put("list",list);
15 param.put("s",s);
16 param.put("d",d);
17 
18 2.执行查询:
19 
20 XXXDao.queryXXX(param);
21 
22 
23 3.在Mybatis的Mapper中是这么写:
24 
25 <select id = "queryXXX",resultType = "XXX", paramterType = "java.util.Map">
26     select * from tab where 1=1
27     <if test="s !=null">
28         and zi_duan1 = #{s}
29     </if>
30     <if test="d !=null">
31         and zi_duan2 = #{d}
32     </if>
33     and zi_duan3 in 
34     <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
35         #{item}  
36     </foreach>
37 </select>
38         
39 //foreach中的collection=“list”  中的list就是上面param参数中list集合的字符串键“list”,即param.put("list",list);中的第一个"list"
40     

 

也就是说,在map.xml文件中,放在map参数中的东西,我们可以在sql语句中直接用存入时的字符串键名来使用

 

posted @ 2017-06-02 16:12  戈博折刀  阅读(603)  评论(0编辑  收藏  举报