foreach 元素的功能非常强大,它允许你指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量。它也允许你指定开头与结尾的字符串以及在迭代结果之间放置分隔符。这个元素是很智能的,因此它不会偶然地附加多余的分隔符。
注意 你可以将任何可迭代对象(如 List、Set 等)、Map 对象或者数组对象传递给 foreach 作为集合参数。当使用可迭代对象或者数组时,index 是当前迭代的次数,item 的值是本次迭代获取的元素。当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值。
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:
- 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list .
- 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array .
- 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.
实体Employees:
public class Employees { private Integer employeeId; private String firstName; private String lastName; private String email; private String phoneNumber; private Date hireDate; private String jobId; private BigDecimal salary; private BigDecimal commissionPct; private Integer managerId; private Short departmentId; }
映射文件:
<!--List:forech中的collection属性类型是List,collection的值必须是:list,item的值可以随意,Dao接口中参数名字随意 --> <select id="getEmployeesListParams" resultType="Employees"> select * from EMPLOYEES e where e.EMPLOYEE_ID in <foreach collection="list" item="employeeId" index="index" open="(" close=")" separator=","> #{employeeId} </foreach> </select> <!--Array:forech中的collection属性类型是array,collection的值必须是:list,item的值可以随意,Dao接口中参数名字随意 --> <select id="getEmployeesArrayParams" resultType="Employees"> select * from EMPLOYEES e where e.EMPLOYEE_ID in <foreach collection="array" item="employeeId" index="index" open="(" close=")" separator=","> #{employeeId} </foreach> </select> <!--Map:不单单forech中的collection属性是map.key,其它所有属性都是map.key,比如下面的departmentId --> <select id="getEmployeesMapParams" resultType="Employees"> select * from EMPLOYEES e <where> <if test="departmentId!=null and departmentId!=''"> e.DEPARTMENT_ID=#{departmentId} </if> <if test="employeeIdsArray!=null and employeeIdsArray.length!=0"> AND e.EMPLOYEE_ID in <foreach collection="employeeIdsArray" item="employeeId" index="index" open="(" close=")" separator=","> #{employeeId} </foreach> </if> </where> </select>
Mapper类:
public interface EmployeesMapper { List<Employees> getEmployeesListParams(List<String> employeeIds); List<Employees> getEmployeesArrayParams(String[] employeeIds); List<Employees> getEmployeesMapParams(Map<String,Object> params); }
测试方法:
@Test public void testGetEmployeesListParams() { List<String> employeeIds = Arrays.asList("100", "101", "200"); List<Employees> result = employeesMapper .getEmployeesListParams(employeeIds); assertEquals(3, result.size()); } @Test public void testGetEmployeesArrayParams() { String[] employeeIds = new String[] { "100", "200" }; List<Employees> result = employeesMapper .getEmployeesArrayParams(employeeIds); assertEquals(2, result.size()); } @Test public void testGetEmployeesMapParams() { String departmentId = "60"; List<String> employeeIdsList = Arrays.asList("103", "104", "105"); String[] employeeIdsArray = new String[] { "103", "104" }; // 传入多个参数 Map<String, Object> params = new HashMap<String, Object>(); params.put("departmentId", departmentId); params.put("employeeIdsList", employeeIdsList); params.put("employeeIdsArray", employeeIdsArray); List<Employees> result = employeesMapper.getEmployeesMapParams(params); assertEquals(3, result.size()); }
MyBatis的foreach语句详解
- 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
- 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
- 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在breast里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key
<select id="dynamicForeachTest" resultType="Blog"> select * from t_blog where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
上述collection的值为list,对应的Mapper是这样的 :
public List<Blog> dynamicForeachTest(List<Integer> ids);
测试代码:
@Test public void dynamicForeachTest() { SqlSession session = Util.getSqlSessionFactory().openSession(); BlogMapper blogMapper = session.getMapper(BlogMapper.class); List<Integer> ids = new ArrayList<Integer>(); ids.add(1); ids.add(3); ids.add(6); List<Blog> blogs = blogMapper.dynamicForeachTest(ids); for (Blog blog : blogs) System.out.println(blog); session.close(); }
2.单参数array数组的类型:
<select id="dynamicForeach2Test" resultType="Blog"> select * from t_blog where id in <foreach collection="array" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
上述collection为array,对应的Mapper代码:
public List<Blog> dynamicForeach2Test(int[] ids);
对应的测试代码:
@Test public void dynamicForeach2Test() { SqlSession session = Util.getSqlSessionFactory().openSession(); BlogMapper blogMapper = session.getMapper(BlogMapper.class); int[] ids = new int[] {1,3,6,9}; List<Blog> blogs = blogMapper.dynamicForeach2Test(ids); for (Blog blog : blogs) System.out.println(blog); session.close(); }
3.自己把参数封装成Map的类型 :
<select id="dynamicForeach3Test" 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); ids.add(7); ids.add(9); 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(); }