Usage Summary of foreach collection in mybatis (three kinds)

Foreach is mainly used to build in conditions. It can iterate a set in SQL statements.

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

Item represents the alias of each element in the collection during iteration. Index specifies a name, which is used to represent the position of each iteration in the iteration process. Open represents what the statement starts with, separator represents what symbol is used as the separator between each iteration, and close represents what it ends with.

When using foreach, the most critical and error prone attribute is the collection attribute, which must be specified, but the value of this attribute is different in different cases. There are three main 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, the attribute value of collection is array 3 If there are multiple parameters passed in, we need to encapsulate them into a map. Of course, a single parameter can also be used

In fact, if you pass in a parameter, it will also be encapsulated into a map in the beast, and the key of the map is the parameter name. Therefore, at this time, the value of the collection attribute is the key of the passed in list or array object in its own encapsulated map. Let's take a look at the example codes of the above three cases:

1. Type of single parameter list:

The value of the above collection is list, and the corresponding mapper is like this

public List dynamicForeachTest(List ids);

Test code:

2. Type of single parameter array:

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

public List dynamicForeach2Test(int[] ids);

Corresponding test code:

3. Encapsulate the parameters into map type

The value of the above collection is IDS, which is the key of the passed in parameter map, and the corresponding mapper Code:

public List dynamicForeach3Test(Map params);

Corresponding test code:

summary

The above is a summary of the usage of foreach collection in mybatis (three kinds) introduced by Xiaobian. I hope it will be helpful to you!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>