<select id="getUser" resultType="com.cat.pojo.User">
SELECT * FROM user
WHERE
<choose>
<when test="id != null and test.trim() != '' ">
id = #{id}
</when>
<when test="name != null and name.trim() != '' ">
name = #{name}
</when>
<otherwise>
age = 17
</otherwise>
</choose>
</select>
如当 id 满足条件时id,只执行id满足条件的语句,后面的when语句都不执行(包括otherwise)。当id 、name都不满足条件时,执行默认语句otherwise。
也能这样写,不要otherwise
<choose>
<when test="address !=null and address == 'other'">
and cc_busi_form.case_address not in ( '天宁区', '新北区', '钟楼区', '经开区', '武进区', '金坛区', '溧阳市')
</when>
<when test="address !=null and address != '' and address != 'other'">
and cc_busi_form.case_address = #{address}
</when>
</choose>