MyBatis的where語句作用

來源:互聯網
上載者:User

where語句的作用主要是簡化SQL語句中where中的條件判斷的,先看一個例子,再解釋一下where的好處。

    <select id="dynamicWhereTest" parameterType="Blog" resultType="Blog">
        select * from t_blog
        <where>
            <if test="title != null">
                title = #{title}
            </if>
            <if test="content != null">
                and content = #{content}
            </if>
            <if test="owner != null">
                and owner = #{owner}
            </if>
        </where>
    </select>
where元素的作用是會在寫入where元素的地方輸出一個where,另外一個好處是你不需要考慮where元素裡面的條件輸出是什麼樣子 的,MyBatis會智能的幫你處理,如果所有的條件都不滿足那麼MyBatis就會查出所有的記錄,如果輸出後是and 開頭的,MyBatis會把第一個and忽略,當然如果是or開頭的,MyBatis也會把它忽略;此外,在where元素中你不需要考慮空格的問 題,MyBatis會智能的幫你加上。像上述例子中,如果title=null, 而content != null,那麼輸出的整個語句會是select * from t_blog where content = #{content},而不是select * from t_blog where and content = #{content},因為MyBatis會智能的把首個and 或 or 給忽略。java-javascript 風之境地

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.