Mybatis中mapper文件中的两层循环

来源:互联网 发布:kx tda200控制软件 编辑:程序博客网 时间:2024/06/01 08:07

Mybatis中mapper文件中的两层循环

导言

使用mapper.xml文件来存储和执行sql语句是Mybatis框架中重要的应用。在mapper.xml文件中对List数据的循环遍历较为普遍和常用,而两层或多层循环是大家不常用的。下面简单介绍一下吧!

从单层循环说起

mapper.java文件中的getData函数 :

 public getData(@("param") List<String> param);

mapper.xml中getData的sql :

 <select id="getData">    select * from table t where t.id in      <foreach item="item" index="index" collection="dataList" open="(" close=")" separator=",">         #{dataList}      </foreach> </select>

分析 :
getData传送过来的是一个List类型的数据,因此需要单层循环即可

两层循环

info类 :

public class Info{  public String name;  public List<String> ids;}

mapper.java中用到info类的函数 :

 public getData(@("param") List<Info>);

mapper.xml中getData的sql :

<select id="getData">    select * from table t where t.id in      <foreach item="item" index="index" collection="dataList">         <foreach item="idItem" index="index" collection="item.ids" open="(" close=")" separator=",">            #{idItem}         </foreach>      </foreach> </select>

注意 :

collection=”item.ids” 为多层循环的关键

后序

以上为Mybaits框架mapper.xml文件使用多层循环一个方面。对于处理map等更为复杂数据的处理,笔者还不知。等待高手指点吧!

0 0