需求:查询的时候,带了带了哪个条件就用哪个条件进行查询,条件之间互斥
可以通过<choose>标签实现:
<select id="getEmpsByConditionChoose" resultType="com.test.beans.Employee">
SELECT * FROM tb1_emplyee
<where>
/*带了那个字段就通过哪个字段进行查询*/
<choose>
<when test="id!=null">
id=#{id}
</when>
<when test="lastName!=null">
last_name like #{lastName}
</when>
<when test="email!=null">
email=#{email}
</when>
<otherwise>
1=1
</otherwise>
</choose>
</where>
</select>