MyBatis The foreach statement in detail list array map

Source: Internet
Author: User

The main use of foreach is in the build in condition, which can iterate a collection in an SQL statement. The properties of the Foreach element are mainly item,index,collection,open,separator,close. The item represents the alias of each element in the collection when it iterates, and index specifies a name that represents the position at which each iteration occurs during the iteration, and open indicates what the statement begins with, and separator indicates what symbol is used as the delimiter between each iteration. Close means the end, the most critical and error-prone when using foreach is the collection property, which must be specified, but in different cases the value of the property is not the same, there are 3 main cases:

1. If a single parameter is passed in and the parameter type is a list, the collection property value is List

2. If a single parameter is passed in and the parameter type is an array, the value of the collection property is array

3. If the parameters passed in are multiple, we need to encapsulate them into a map, of course, the single parameter can also be encapsulated as a map, in fact, if you pass in the parameter, in the breast will also wrap it into a map, The key to the map is the parameter name, so this time the collection property value is the key to the incoming list or array object in its own encapsulated map

Here's a look at the sample code for each of the three scenarios:

1. Type of single parameter list:

<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>

The value of the above collection is list, the corresponding Mapper is this

 Public List<blog> dynamicforeachtest (list<integer> IDs);

Test code:

@Test Public voiddynamicforeachtest () {sqlsession session=util.getsqlsessionfactory (). Opensession (); Blogmapper Blogmapper= Session.getmapper (blogmapper.class); List<Integer> ids =NewArraylist<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. Type of single-parameter array arrays:

<SelectId="Dynamicforeach2test" Resulttype="Blog">        Select *  fromT_blogwhereIdinch        <foreach Collection="Array"Index="IndexThe item="Item"Open="(" Separator=","Close=")">#{item}</Foreach></Select>

The above collection is an array, corresponding to the mapper code:

 Public List<blog> dynamicforeach2test (int[] IDs);

The corresponding test code:

@Test      Public void dynamicforeach2test () {        = util.getsqlsessionfactory (). Opensession ();         = Session.getmapper (blogmapper.  Class);         int New int [] {1,3,6,9};        List<Blog> blogs = blogmapper.dynamicforeach2test (IDs);          for (Blog blog:blogs)            SYSTEM.OUT.PRINTLN (blog);        Session.close ();    }

3. Encapsulate the parameters as a map type

<SelectId="Dynamicforeach3test" Resulttype="Blog">        Select *  fromT_blogwhereTitle like"%"#{title}"%" andIdinch        <foreach Collection="IDs"Index="IndexThe item="Item"Open="(" Separator=","Close=")">#{item}</Foreach></Select>

The value of the above collection is IDs, which is the key of the incoming parameter map, corresponding to the mapper code:

 Public List<blog> dynamicforeach3test (map<string, object> params);

Corresponding Test code:

@Test Public voiddynamicforeach3test () {sqlsession session=util.getsqlsessionfactory (). Opensession (); Blogmapper Blogmapper= Session.getmapper (blogmapper.class); Finallist<integer> ids =NewArraylist<integer>(); Ids.add (1); Ids.add (2); Ids.add (3); Ids.add (6); Ids.add (7); Ids.add (9); Map<string, object> params =NewHashmap<string, object>(); Params.put ("IDs", IDS); Params.put ("title", "China"); List<Blog> Blogs =blogmapper.dynamicforeach3test (params);  for(blog blog:blogs) System.out.println (blog);    Session.close (); }

MyBatis The foreach statement in detail list array map

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.