MyBatis Incoming Collection list array map parameter notation

Source: Internet
Author: User

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 practice by code:

Data sheet:

Using Oracle's Hr.employees table

Entity: Employees

?
12345678910111213 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:

?
1234567891011121314151617181920212223242526272829303132333435363738 <!--List:forech中的collection属性类型是List,collection的值必须是:list,item的值可以随意,Dao接口中参数名字随意 --><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><!--Array:forech中的collection属性类型是array,collection的值必须是:list,item的值可以随意,Dao接口中参数名字随意 --><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:不单单forech中的collection属性是map.key,其它所有属性都是map.key,比如下面的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:

?
12345 publicinterfaceEmployeesMapper {   List<Employees> getEmployeesListParams(List<String> employeeIds);  List<Employees> getEmployeesArrayParams(String[] employeeIds);  List<Employees> getEmployeesMapParams(Map<String,Object> params);}

The above is a small series to introduce you to the MyBatis incoming collection list array map parameters of the whole narrative, I hope to help you!

MyBatis Incoming Collection list array map parameter notation

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.