web123456

Three usages of foreach collection in mybatis

Source of reprint:/fangyu19900812/p/
refer to:/ruiati/p/
Foreach is mainly used in building in conditions, it can iterate over a collection in SQL statements.

The main attributes of the foreach element are item, index, collection, open, separator, and close.

Item represents the alias when each element in the set is iterated.
 index specifies a name to represent the position to which each iteration is reached during the iteration process.
 open means where the statement starts,
 separator indicates what symbol is used as the separator between each iteration.
 close indicates what ends.

When using foreach, the most critical and most error-prone thing is the collection attribute. This attribute must be specified, but in different cases, the value of this attribute is different. There are three main situations:

1. If the passed in a single parameter and the parameter type is a List, the collection attribute value is list
 2. If the passed in a single parameter and the parameter type is an array, the property value of the collection is array
 3. If there are multiple parameters passed in, we need to encapsulate them into a map, of course, single parameters can also be

To encapsulate it into a map, in fact, if you pass in the parameters, it will also encapsulate it into a map in the breast. The map's key is the parameter name, so at this time the collection attribute value is the key of the passed List or array object in the map encapsulated by itself. Let's take a look at the example code of the above three situations:
1. Type of single parameter List:

1 <select id="dynamicForeachTest" parameterType="" resultType="Blog">
2           select * from t_blog where id in
3        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
4                #{item}       
5        </foreach>    
6    </select>

The value of the above collection is list, and the corresponding mapper is like this
public List dynamicForeachTest(List ids);
Test code:

 1 @Test
 2     public void dynamicForeachTest() {
 3         SqlSession session = Util.getSqlSessionFactory().openSession();      
 4         BlogMapper blogMapper = session.getMapper(BlogMapper.class);
 5          List ids = new ArrayList();
 6          ids.add(1);
 7          ids.add(3);
 8           ids.add(6);
 9         List blogs = blogMapper.dynamicForeachTest(ids);
10          for (Blog blog : blogs)
11              System.out.println(blog);
12          session.close();
13      }

2. Type of single parameter array:

 1 <select id="dynamicForeach2Test" parameterType="" resultType="Blog">
2     select * from t_blog where id in
3     <foreach collection="array" index="index" item="item" open="(" separator="," close=")">
4          #{item}
5     </foreach>
6 </select>    

The above collection is array, corresponding Mapper code:
public List dynamicForeach2Test(int[] ids);
Corresponding test code:

 1 @Test
 2 public void dynamicForeach2Test() {
 3         SqlSession session = ().openSession();
 4         BlogMapper blogMapper = ();
 5         int[] ids = new int[] {1,3,6,9};
 6         List blogs = blogMapper.dynamicForeach2Test(ids);
 7         for (Blog blog : blogs)
 8         System.out.println(blog);    
 9         ();
10 }

3. Encapsulate the parameters into the type of Map by yourself

1 <select id="dynamicForeach3Test" parameterType="" resultType="Blog">
2         select * from t_blog where title like "%"#{title}"%" and id in
3          <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
4               #{item}
5          </foreach>
6 </select>

The value of the above collection is ids, which is the key of the parameter Map passed in, and the corresponding Mapper code:
public List dynamicForeach3Test(Map params);
Corresponding test code:

@Test
    public void dynamicForeach3Test() {
        SqlSession session = Util.getSqlSessionFactory().openSession();
         BlogMapper blogMapper = session.getMapper(BlogMapper.class);
          final List ids = new ArrayList();
          ids.add(1);
          ids.add(2);
          ids.add(3);
          ids.add(6);
         ids.add(7);
         ids.add(9);
        Map params = new HashMap();
         params.put("ids", ids);
         params.put("title", "China");
        List blogs = blogMapper.dynamicForeach3Test(params);
         for (Blog blog : blogs)
             System.out.println(blog);
         session.close();
     }