MyBatis incoming parameters as a collection list array map 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:

    1. If a single parameter is passed in and the parameter type is a list, the collection property value is List.
    2. If a single parameter is passed in and the parameter type is an array, the value of the collection property is Array.
    3. 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.
      1. 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

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.