select * from employee where
<if test="id!=null">
id=#{id}
</if>
<if test="name!=null and name!=""">
and name=#{name}
</if>
<if test="email!=null and email!=""">
and email=#{email}
</if>
此时如果传过来的id为空,此时会多出一个 “and”(去掉此and有三种方法 where标签,还可以在where后直接加 1=1,再就是使用trim标签)
利用trim()标签可有效覆盖
select * from employee
<trim prefix="where" prefixOverrides="and">
<if test="id!=null">
id=#{id}
</if>
<if test="name!=null and name!=""">
and name=#{name}
</if>
<if test="email!=null and email!=""">
and email=#{email}
</if>
</trim>
prefix =" ",给整体添加前缀
prefixOverrides=“ ”去掉前面多余字符
suffix=“ ”给整体添加后缀
suffixOverrides=" "去掉后面多余字符