mybatis動態sql中where標簽的使用


where標記的作用類似於動態sql中的set標記,他的作用主要是用來簡化sql語句中where條件判斷的書寫的,如下所示:

  <select id="selectByParams" parameterType="map" resultType="user">

    select * from user

    <where>

      <if test="id != null ">id=#{id}</if>

      <if test="name != null and name.length()>0" >and name=#{name}</if>

      <if test="gender != null and gender.length()>0">and gender = #{gender}</if>

    </where>

  </select>     

  在上述SQL中加入ID的值為null的話,那么打印出來的SQL為:select * from user where name="xx" and gender="xx"

  where 標記會自動將其后第一個條件的and或者是or給忽略掉


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM