温馨提示×

mybatis判断集合为空的方法是什么

小亿
129
2024-02-28 14:13:16
栏目: 编程语言

在MyBatis中,可以使用OGNL表达式语言中的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语句。

0