MyBatis's foreach query (List, Array, Map)

Three uses of foreach collection in mybatis

The main use of foreach is to build in conditions, which can iterate over a collection in a SQL statement.

The attributes of the foreach element mainly include item, index, collection, open, separator, and close.

    item represents the alias of each element in the collection when iterating,
    index specifies a name, which is used to represent the position of each iteration during the iteration process,
    open represents what the statement starts with, and
    separator represents between each iteration What symbol is used as the delimiter, and
    close means what to end with.

 

The most critical and error-prone when using foreach is the collection attribute, which must be specified, but in different cases, the value of this attribute is different, mainly in the following three cases:

    1. When the input is a single parameter and the parameter type is a List, the collection property value is list
    2. If the input is a single parameter and the parameter type is an array array, the collection property value is array
    3. If the input is an array When there are multiple parameters, we need to encapsulate them into a Map. Of course, a single parameter can also be encapsulated into a map. In fact, if you pass in a parameter, it will also be encapsulated into a Map in MyBatis. , the key of the map is the parameter name, so at this time the value of the collection attribute is the key of the incoming List or array object in the map encapsulated by itself

 

Let's take a look at the sample code for the above three cases:

1. The type of single parameter List

<!--List: The type of the collection attribute in the forech is List, the value of the collection must be: list, the value of the item can be arbitrary, and the parameter name in the Dao interface 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>

 

2. The type of single-parameter array array

<!--Array: The collection property type in forech is array, the value of collection must be: list, the value of item can be arbitrary, the parameter name in Dao interface 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>

 

3. Encapsulate the parameters into the type of Map

 

<!--Map: Not only the collection attribute in forech is map.key, but all other attributes 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>

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326450696&siteId=291194637