MyBatis's foreach statement is detailed _java

Source: Internet
Author: User

The main use of foreach is in the build in condition, which can iterate over a collection in an SQL statement. The properties of a foreach element are mainly item,index,collection,open,separator,close. Item represents the alias for each element of the collection when it is iterated, and index specifies a name that represents where each iteration is in the iteration, open indicates what the statement starts with, and separator represents what symbol to use as a separator between iterations, Close indicates what ends with foreach, and the most critical and error-prone is the collection property, which must be specified, but in different cases the value of the property is not the same, mainly for 3 things:

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 collection property value is array

3. If the incoming parameters are more than one, we need to encapsulate them into a map, of course, single parameter can be encapsulated into a map, in fact, if you pass the parameter, in the breast will be encapsulated in a map, the map key is the parameter name, So at this point, the collection attribute value is the key of the list or array object that is passed in its own encapsulated map.

Let's look at the sample code for the above three cases, respectively:

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, and the corresponding Mapper is this

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

Test code:

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

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

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

The corresponding test code:

@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. Encapsulate the parameters into the type of 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>

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
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", "China");
list<blog> blogs = blogmapper.dynamicforeach3test (params);
for (blog blog:blogs)
System.out.println (blog);
Session.close ();
}

The above is a small set of mybatis to introduce the relevant knowledge of the foreach statement, I hope to help you!

Related Article

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.