<selectid="getEmpsByConditionIf"resultType="com.mybatis.beans.Employee">
select id , last_name ,email , gender
from tbl_employee
<where><iftest="id!=null">
and id = #{id}
</if><iftest="lastName!=null && lastName!=""">
and last_name = #{lastName}
</if><iftest="email!=null and email.trim()!=''">
and email = #{email}
</if><iftest=""m".equals(gender) or "f".equals(gender)">
and gender = #{gender}
</if></where></select>
<selectid="getEmpsByConditionTrim"resultType="com.mybatis.beans.Employee">
select id , last_name ,email , gender
from tbl_employee
<trimprefix="where"suffixOverrides="and"><iftest="id!=null">
id = #{id} and
</if><iftest="lastName!=null && lastName!=""">
last_name = #{lastName} and
</if><iftest="email!=null and email.trim()!=''">
email = #{email} and
</if><iftest=""m".equals(gender) or "f".equals(gender)">
gender = #{gender}
</if></trim></select>
4.set
set 主要是用于解决修改操作中SQL语句中可能多出逗号的问题
<updateid="updateEmpByConditionSet">
update tbl_employee
<set><iftest="lastName!=null && lastName!=""">
last_name = #{lastName},
</if><iftest="email!=null and email.trim()!=''">
email = #{email} ,
</if><iftest=""m".equals(gender) or "f".equals(gender)">
gender = #{gender}
</if></set>
where id =#{id}
</update>
<selectid="getEmpsByConditionForeach"resultType="com.mybatis.beans.Employee">
select id , last_name, email ,gender from tbl_employee where id in
<foreachcollection="ids"item="curr_id"open="("close=")"separator=",">
#{curr_id}
</foreach></select>