<selectid="selectStudentIf"resultType="org.example.domain.Student" > select * from student where <!--只有当Java对象传来的参数name不为空,且不为空字符串时,才会加入该语句--> <iftest="name != null and name != '' " > name = #{name} </if> <!--当age大于零时,拼接下面的sql语句--> <iftest="age > 0" > and age > #{age} </if>
<selectid="selectStudentIf"resultType="org.example.domain.Student" > select * from student where 1 = 1 <iftest="name != null and name != '' " > and name = #{name} </if> <iftest="age > 0" > and age > #{age} </if>
<selectid="selectStudentsByIds"resultType="org.example.domain.Student"> select * from student where id in <foreachcollection="list"item="id"open="("close=")"separator="," > #{id} </foreach> </select>
<selectid="selectStudentsByIds"resultType="org.example.domain.Student"> select * from student where id in <foreachcollection="list"item="student"open="("close=")"separator="," > #{student.id} </foreach> </select>
<selectid="selectStudentsByIds"resultType="org.example.domain.Student"> <includerefid="select" /> where id in <foreachcollection="list"item="student"open="("close=")"separator="," > #{student.id} </foreach> </select>