Mybatis <if>标签

格式:<if test=""> sql语句 </if>

<select id="selectByName" resultType="Person" parameterType="java.lang.String" >  
    select * from person
    <if test="_parameter != null">  
        where name = #{name}  
    </if>  
</select>  

注意:Mybatis动态SQL单一基础类型参数用if标签时,test中应该用 _parameter

如果是类变量

<select id="selectByName" resultType="Person" parameterType="Person" >  
    select * from person
    <where>
          <if test="age!=0">
                age=#{age}
          </if>
          <if test="gender!=null">
                and gender=#{gender}
          </if>
    </where>
    
</select>  
原文地址:https://www.cnblogs.com/caoyc/p/5574921.html