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:
If a single parameter is passed in and the parameter type is a list, the collection property value is list.
If a single argument is passed in and the parameter type is an array, the collection property value is array.
If the incoming arguments are multiple, we need to encapsulate them into a map, and of course a single parameter can be encapsulated as a map, in fact if you're passing in the parameter, in MyBatis it will be encapsulated into a map, the map key is the parameter name, So this time the collection attribute value is the key of the incoming list or array object in its own encapsulated map.
Here's what we do with code:
Data table:
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;
Mapping File:
The collection attribute type in the <!--List:forech is list,collection value must be: List,item value can be arbitrary, the DAO interface parameter name arbitrarily--> <select " Getemployeeslistparams "resulttype=" Employees "> select * from Employees e where e.employee_id in <fo
Reach collection= "list" item= "EmployeeId" index= "index" open= "(" close= ")" separator= "," > #{employeeid} " </foreach> </select> <!--Array:forech Collection attribute type is array,collection value must be: List,item value can be arbitrary,
The name of the parameter in the DAO interface is random--> <select id= "Getemployeesarrayparams" resulttype= "Employees" > select * from Employees E where e.employee_id in <foreach collection= "array" item= "EmployeeId" index= "index" open= "(" close= ") se Parator= "," > #{employeeid} </foreach> </select> <!--Map: Not only the collection attribute in Forech is Map.key, its
All of its 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> & Lt;if test= "Employeeidsarray!=null and Employeeidsarray.length!=0" > and e.employee_id in <foreach Co llection= "Employeeidsarray" item= "employeeId" index= "index" open= "(" close= ")" separator= "," > #{emp Loyeeid} </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);
}
The above is a small set to introduce the MyBatis incoming collection list array map parameters of the entire description, I hope to help!