foreach元素的属性主要有 item,index,collection,open,separator,close。
在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况:
下面分别来看看上述三种情况的示例代码:
1.单参数List的类型:
上述collection的值为list,对应的Mapper是这样的
测试代码:
2.单参数array数组的类型:
上述collection为array,对应的Mapper代码:
public List dynamicForeach2Test(int[] ids);
对应的测试代码:
<span class="hljs-number">1 @Test
<span class="hljs-number">2 <span class="hljs-keyword">public <span class="hljs-keyword">void <span class="hljs-title">dynamicForeach2Test() {
<span class="hljs-number">3 SqlSession session = Util.getSqlSessionFactory().openSession();
<span class="hljs-number">4 BlogMapper blogMapper = session.getMapper(BlogMapper.class);
<span class="hljs-number">5 <span class="hljs-keyword">int[] ids = <span class="hljs-keyword">new <span class="hljs-keyword">int[] {<span class="hljs-number">1,<span class="hljs-number">3,<span class="hljs-number">6,<span class="hljs-number">9};
<span class="hljs-number">6 List blogs = blogMapper.dynamicForeach2Test(ids);
<span class="hljs-number">7 <span class="hljs-keyword">for (Blog blog : blogs)
<span class="hljs-number">8 System.<span class="hljs-keyword">out.println(blog);
<span class="hljs-number">9 session.close();
<span class="hljs-number">10 }</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
3.自己把参数封装成Map的类型
<span class="hljs-number">1 <<span class="hljs-keyword">select id=<span class="hljs-string">"dynamicForeach3Test" parameterType=<span class="hljs-string">"java.util.HashMap" resultType=<span class="hljs-string">"Blog">
<span class="hljs-number">2 <span class="hljs-keyword">select * <span class="hljs-keyword">from t_blog <span class="hljs-keyword">where title like <span class="hljs-string">"%"<span class="hljs-preprocessor">#{title}"%" and id in
<span class="hljs-number">3 <<span class="hljs-keyword">foreach collection=<span class="hljs-string">"ids" index=<span class="hljs-string">"index" item=<span class="hljs-string">"item" open=<span class="hljs-string">"(" separator=<span class="hljs-string">"," close=<span class="hljs-string">")">
<span class="hljs-number">4 <span class="hljs-preprocessor">#{item}
<span class="hljs-number">5 foreach>
<span class="hljs-number">6 select></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
- 1
- 2
- 3
- 4
- 5
- 6
上述collection的值为ids,是传入的参数Map的key,对应的Mapper代码:
public List dynamicForeach3Test(Map params);
对应测试代码:
@Test
public void dynamicForeach3Test() {
SqlSession session = Util<span class="hljs-preprocessor">.getSqlSessionFactory()<span class="hljs-preprocessor">.openSession()</span></span>
Original: https://www.cnblogs.com/toSeeMyDream/p/12381217.html
Author: nickTimer
Title: mybatis 中 foreach collection的三种用法
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/543367/
转载文章受原作者版权保护。转载请注明原作者出处!