MyBatis コレクションが空か判定する方法【isEmpty活用】

MyBatisでは、isEmpty()関数を使用して、コレクションが空かどうかを判断することができます。

MyBatisのselect文を使用して、コレクションが空であるかどうかを判断することができます。

<select id="selectUsers" resultMap="userResultMap">
    SELECT * FROM users
    <where>
        <if test="userIds != null and !userIds.isEmpty()">
            AND user_id IN
            <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
                #{userId}
            </foreach>
        </if>
    </where>
</select>

上記の例では、userIdsはList型のパラメータであり、isEmpty()関数を使用して空かどうかを判断し、空でない場合は対応するSQL文を実行します。

bannerAds