<where>标签会在sql语句中加上 'where',并且会去掉'and'(使用where标签的话,and就要放在语句的前面,比如'and ename = #{ename}', 如果放在语句的后面,比如 'ename = #{ename} and' , where标签可能去不掉and,就会造成拼接的sql语句最后多一个and,造成错误)
<select id="getEmpsByConditionIf" resultType="com.atChina.bean.Employee">
select * from(
select a.empno as no, a.ename, a.job, a.mgr,
a.hiredate, a.sal, a.deptno from emptest a
)
<where>
<if test="empno!=null">
no = #{empno}
</if>
<if test="ename != null and ename.trim() !='' ">
and ename = #{ename}
</if>
<if test="job !=null and job !='' ">
and job = #{job}
</if>
</where>
</select>