Mybatis框架中mapper配置和foreach标签的使用

foreach标签

下面介绍一下一个mapper配置文件中的foreach标签(注意,要跟着前面的总结来看,这里使用的例子是结合前面的工程写的,大部分代码没有再赘述)

foreach的作用是向sql传递数组或List,mybatis使用foreach解析

1.1需求
在用户查询列表和查询总数的statement中增加多个id输入查询。
sql语句如下:

两种方法:
SELECT * FROM USER WHERE id=1 OR id=3 OR id=5

SELECT * FROM USER WHERE id IN(1,3,5)

1.2在输入参数类型中添加List ids传入多个id
public class UserQueryVo {

//传入多个id
private List ids;

public List getIds() {
return ids;
}

public void setIds(List ids) {
this.ids = ids;
}
......
}

1.3修改mapper.xml

WHERE id=1 OR id=3 OR id=5
在查询条件中,查询条件定义成一个sql片段,需要修改sql片段。
[html]  view plain  copy
  1. <pre name="code" class="html">
  2.   
  3.             id=#{user_id}  
  4.         foreach>  
  5.     if>  
  6. sql>  
  7.   
  8.   
  9.     <select id="findUserList" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo"   
  10.                                 resultType="cn.edu.hpu.mybatis.PO.UserCustom">  
  11.         select * from user   
  12.           
  13.             
  14.         <where>  
  15.               
  16.             <include refid="query_user_where">include>  
  17.               
  18.         where>  
  19.     select>  
 在mapper接口类中添加相应方法: 
[java]  view plain  copy
  1. //用户管理的Dao接口  
  2. public interface UserMapper {  
  3.       
  4.     //用户信息综合查询  
  5.     public List findUserList(UserQueryVo userQueryVo) throws Exception;  
  6.     ......  
  7. }  

1.4测试代码
[java]  view plain  copy
  1. //用户信息综合查询  
  2.     @Test  
  3.     public void testFindUserList() throws Exception{  
  4.           
  5.         SqlSession sqlSession=sqlSessionFactory.openSession();  
  6.           
  7.         //创建UserMapper代理对象  
  8.         UserMapper userMapper=sqlSession.getMapper(UserMapper.class);  
  9.           
  10.         //创建包装对象,设置查询条件  
  11.         UserQueryVo userQueryVo=new UserQueryVo();  
  12.         //传入多个Id  
  13.         List ids=new ArrayList();  
  14.         ids.add(1);  
  15.         ids.add(3);  
  16.         ids.add(5);  
  17.         //将ids通过userQueryVo传入statement中  
  18.         userQueryVo.setIds(ids);  
  19.           
  20.         //调用userMapper的方法  
  21.         List users=userMapper.findUserList(userQueryVo);  
  22.           
  23.         for (int i = 0; i < users.size(); i++) {  
  24.             UserCustom user=(UserCustom)users.get(i);  
  25.             System.out.println(user.getId()+":"+user.getUsername());  
  26.         }  
  27.     }  

测试结果:
1:张三
3:刘莉莉
5:刘三姐


日志输出:
[plain]  view plain  copy
  1. DEBUG [main] - Opening JDBC Connection  
  2. DEBUG [main] - Created connection 6867819.  
  3. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@68cb6b]  
  4. DEBUG [main] - ==>  Preparing: select * from user WHERE ( id=? OR id=? OR id=? )   
  5. DEBUG [main] - ==> Parameters: 1(Integer), 3(Integer), 5(Integer)  
  6. DEBUG [main] - <==      Total: 3  

可以看到,sql语句select * from user WHERE ( id=? OR id=? OR id=? ) 通过foreach输出成功


1.5另外一个sql的实现:
[html]  view plain  copy
  1.   
  2.     #{user_id}  
  3. foreach>  

你可能感兴趣的:(Mybatis,foreach,mybatis,mapper)