在 MyBatis 中,可以使用以下方法来判断一个 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>
<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 语句。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>