代码
<select id="getStudents" resultType="student" parameterType="map">
select * from mybatis_test.student
<where>
<choose>
<when test="id != null">
id = #{id}
</when>
<when test="name != null">
and name = #{name}
</when>
<otherwise>
and tid = #{tid}
</otherwise>
</choose>
</where>
</select>
代码讲解
- choose标签,when标签,otherwise标签组合起来就相当于java中的switch语句。
- 当满足其中一个when标签中的条件时,将不再执行其他的when标签和otherwise标签
- 当所有的when标签中的条件均未满足时,执行othewise标签