<select id="queryUserBySQLTag" resultType="org.entity.user" parameterType="org.entity.user">
select usename ,type,gender from user where
<if test="usename!=null">
usename=#{usename}
</if>
<if test="phone!=null">
and phone=#{phone}
</if>
</select>
如果第一个if,usename不存在,那么就变成了where and xxxxx,会报错
解决方法一:
<select id="queryUserBySQLTag" resultType="org.entity.user" parameterType="org.entity.user">
select usename ,type,gender from user where 1=1
<if test="usename!=null">
and usename=#{usename}
</if>
<if test="phone!=null">
and phone=#{phone}
</if>
</select>
开始加一个1=1的条件,之后每一个if中语句都加上and
解决方法二:<where>
<select id="queryUserBySQLTag" resultType="org.entity.user" parameterType="org.entity.user">
select usename ,type,gender from user
<where>
<if test="usename!=null">
and usename=#{usename}
</if>
<if test="phone!=null">
and phone=#{phone}
</if>
</where>
</select>
<where>会自动处理第一个if中的and,但不会处理其他<if>的and