foreach tag of MyBatis Mapper file for transmission over Lise iterative user or Array, or pieced together to make the batch processing data according to iteration. Such as: using foreach to splice in sub statement.
In learning foreach tag MyBatis Mapper file we look at how the DTD is defined? DTD code is as follows:
1
2
3
4
5
6
7
8
9
10
|
<!
-- 定义foreach元素 -->
<!ELEMENT foreach (#PCDATA | include | trim |
where
|
set
| foreach | choose | if | bind)*>
<!
-- 定义foreach元素的属性 -->
<!ATTLIST foreach
collection CDATA #REQUIRED
item CDATA #IMPLIED
index
CDATA #IMPLIED
open
CDATA #IMPLIED
close
CDATA #IMPLIED
separator CDATA #IMPLIED>
|
Detailed property foreach
collection
This attribute specifies the name of the collection you want to traverse, and then when you call this method, the passed through parameters.
It defines a method for obtaining user information, and then used to construct foreach statement in the sub-element. As used herein, the default Array, an array can be passed to this method as follows:
userMapper.getUserInfo(new String[]{"val01", "val02", "val03", "val04"});
note:
(1) List the default name for the "list".
The default name (2) Array (Array) for "array". Such as:
<select id="getUserInfo" resultType="hashmap"> SELECT N_USERID, C_NAME, C_SEX, N_AGE FROM T_USER WHERE N_USERID IN <foreach item="myItem" index="index" collection="array" open="(" separator="," close=")" > #{myItem} </foreach> </select>
(3) If the specified name is not "list" and "array", then you need to Map <String, List> or Map <String, new Object []> way to specify the name.
Map<String,List<String>> params = new HashMap<String,List<String>>(); params.put("params", Arrays.asList(new String[]{"val01", "val02", "val03", "val04"}) ); userMapper.getUserInfo(params );
GetUserInfo above defines a method used to obtain user information, and then use the foreach element is constructed in sub-statement, where we use a custom myList as a set of parameters.
<select id="getUserInfo" parameterType="hashmap" resultType="hashmap"> SELECT N_USERID, C_NAME, C_SEX, N_AGE FROM T_USER WHERE N_USERID IN <foreach item="myItem" index="index" collection="myList" open="(" separator="," close=")" > #{myItem} </foreach> </select>
item
Iteration for temporarily storing the set value of the current element in the ease of use of the foreach. Such as:
<select id="getUserInfo" parameterType="hashmap" resultType="hashmap"> SELECT N_USERID, C_NAME, C_SEX, N_AGE FROM T_USER WHERE N_USERID IN <foreach item="myItem" index="index" collection="myList" open="(" separator="," close=")" > #{myItem} </foreach> </select>
In the above statement item = "myItem", this value # {myItem} is used.
index
This attribute is used to specify the subject of the next set of iterations used to access the name. Such as: index = "myIndex", # {myIndex} is used to access the current iteration index. Subscript starts at 0.
open
The value of the specified attribute added to the beginning of the string after the spell foreach iteration. Such as: child put together in the beginning of the statement. "(."
close
The value of the specified attribute to the end of the string foreach iteration spell. Such as: put together in sub-section describes the statement ")."
separator
Foreach element for each divided element of iterations. Such as: val1, val2, val3
<select id="getUserInfo" parameterType="hashmap" resultType="hashmap"> SELECT N_USERID, C_NAME, C_SEX, N_AGE FROM T_USER WHERE N_USERID IN <foreach item="myItem" index="index" collection="myList" open="(" separator="," close=")" > #{myItem} </foreach> </select>
If the above process is passed to the array is
String [] Array = new new String [] { "val1 is", "val2", "VAL3"}; userMapper.getUserInfo (Array); // put together the string: (val1, val2, val3)