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:
- If a single parameter is passed in and the parameter type is a list, the collection property value is List.
- If a single parameter is passed in and the parameter type is an array, the value of the collection property is Array.
- 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 MyBatis will also wrap it into a map, the map key 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.
- Below we pass code practice: data sheet: using Oracle's hr.employees table Entity: Employees
public class Employees {
Private Integer employeeId;
Private String firstName;
Private String lastName;
Private String email;
Private String phonenumber;
Private Date hiredate;
Private String jobId;
Private BigDecimal salary;
Private BigDecimal commissionpct;
Private Integer managerid;
Private short departmentid;
}
Map File:
<!--the collection attribute type in List:forech is the value of list,collection must be: List,item value can be arbitrary, DAO interface parameter name is arbitrary--
<select id= "getemployeeslistparams" resulttype= "Employees" >
SELECT *
From EMPLOYEES E
where e.employee_id in
<foreach collection= "list" item= "employeeId" index= "index"
open= "(" close= ")" separator= "," >
#{employeeid}
</foreach>
</select>
<!--the collection attribute type in Array:forech is the value of array,collection must be: List,item value can be arbitrary, DAO interface parameter name is arbitrary--
<select id= "getemployeesarrayparams" resulttype= "Employees" >
SELECT *
From EMPLOYEES E
where e.employee_id in
<foreach collection= "array" item= "employeeId" index= "index"
open= "(" close= ")" separator= "," >
#{employeeid}
</foreach>
</select>
<!--Map: not only the collection attribute in Forech is map.key, all other properties are map.key, such as the following departmentid-
<select id= "getemployeesmapparams" resulttype= "Employees" >
SELECT *
From EMPLOYEES E
<where>
<if test= "departmentid!=null and departmentid!=" >
e.department_id=#{departmentid}
</if>
<if test= "employeeidsarray!=null and employeeidsarray.length!=0" >
and e.employee_id in
<foreach collection= "employeeidsarray" item= "employeeId"
index= "index" open= "(" close= ")" separator= "," >
#{employeeid}
</foreach>
</if>
</where>
</select>
Mapper Class:
Public interface Employeesmapper {
list<employees> Getemployeeslistparams (list<string> employeeids);
list<employees> Getemployeesarrayparams (string[] employeeids);
list<employees> Getemployeesmapparams (map<string,object> params);
} http://blog.csdn.net/small____fish/article/details/8029030
MyBatis incoming parameters as a collection list array map notation