How can I skip a query and return an empty list if ids is empty?
To skip a request (do not execute it), just do not call Mybatis. The calling code should check for identifiers:
return null == ids || ids.isEmpty() ? new ArrayList<User>() : session.select("getByIds", ids);
This is exactly what is being asked in the question.
, Mybatis , , ( ), . , - id = <!-- a value that will never exist in the table -->, , , ( ) .
:
WHERE
<choose>
<when test="ids==null || ids.isEmpty()">
1 = 0
</when>
<otherwise>
id IN <foreach item="id" collection="ids" open="(" separator="," close=")">#{id}</foreach>
</otherwise>
</choose>
"" , , .