MyBatis the _java of the map parameter of the Passed-in collection list array

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:
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> &Lt;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!

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.