mybatis中的where


在多個查詢條件下,由於需要拼接sql語句,所以會在前面加上 where 1 = 1

   select id,name,gender,email from emp
       where 1 = 1             <if test="id != null and id != ''">
                 and id =  #{id}
            </if>
           <if test="name != null and name != ''">
                and name = #{name}
           </if>
    </select>

 可以使用<where></where>代替:

 select id,name,gender,email from emp
       <where>
           <if test="id != null and id != ''">
               and id = #{id}
           </if>
           <if test="name != null and name != ''">
               and name = #{name}
           </if>
       </where>

還可以使用<trim></trim>代替:

trim標簽:

1》prefix="":前綴:trim標簽體中是整個字符串拼串 后的結果,prefix給拼串后的整個字符串加一個前綴
2》prefixOverrides="":前綴覆蓋: 去掉整個字符串前面多余的字符
3》suffix="":后綴,suffix給拼串后的整個字符串加一個后綴
4》suffixOverrides=""后綴覆蓋:去掉整個字符串后面多余的字符

  select id,name,gender,email from emp
       <trim prefix="where" prefixOverrides="and">
           <if test="id != null and id != ''">
               and id =  #{id}
           </if>
           <if test="name != null and name != ''">
               and name = #{name}
           </if>
       </trim>

 


免責聲明!

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



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