在 MyBatis 中,可以使用以下方法来判断一个 List 是否为空:
使用 OGNL 表达式判断 List 是否为空:<if test="list != null and list.size() > 0"> SELECT * FROM table WHERE id IN <foreach collection="list" item="item" open="(" separator="," close=")"> #{item} </foreach></if>使用动态 SQL 标签进行判断:<choose> <when test="list != null and list.size() > 0"> SELECT * FROM table WHERE id IN <foreach collection="list" item="item" open="(" separator="," close=")"> #{item} </foreach> </when> <otherwise> SELECT * FROM table </otherwise></choose>这样可以根据 List 是否为空来动态生成相应的 SQL 语句。如果 List 为空,则不会执行相应的 SQL 语句。

