Reprinted: Three uses of foreach collection in mybatis

The main use of foreach is to build in conditions, which can iterate over a collection in a SQL statement.

The attributes of the foreach element mainly include item, index, collection, open, separator, and close.

    item represents the alias of each element in the collection when iterating,
    index specifies a name to indicate the position of each iteration during the iteration process,
    open indicates what the statement begins with,
    separator indicates what symbol is used as the separator between each iteration,
    close means what to end with.


The most critical and error-prone when using foreach is the collection attribute, which must be specified, but in different cases, the value of this attribute is different, mainly in the following three cases:

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

It can be encapsulated into a map. In fact, if you pass in a parameter, it will also be encapsulated into a Map in breast. The key of the map is the parameter name, so the collection attribute value is the incoming List or array object at this time. Let's take a look at the sample code of the above three cases under the key in the map you encapsulated:
1. The type of single parameter List:
  

1 <select id="dynamicForeachTest" parameterType="java.util.List" 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:

copy 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. The type of the single-parameter array array:

1 <select id="dynamicForeach2Test" parameterType="java.util.ArrayList" 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 an array, and the corresponding Mapper code:
public List dynamicForeach2Test(int[] ids);
Corresponding test code:
      

copy code
 1 @Test
 2 public void dynamicForeach2Test() {
 3         SqlSession session = Util.getSqlSessionFactory().openSession();
 4         BlogMapper blogMapper = session.getMapper(BlogMapper.class);
 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         session.close();
10 }
3. Encapsulate the parameters into the type of Map yourself

1 <select id="dynamicForeach3Test" parameterType="java.util.HashMap" 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 incoming parameter Map, and the corresponding Mapper code:
public List dynamicForeach3Test(Map params);
Corresponding test code:
    

copy 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", "中国");
        List blogs = blogMapper.dynamicForeach3Test(params);
         for (Blog blog : blogs)
             System.out.println(blog);
         session.close();
     }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324843448&siteId=291194637