Mybatis動態sql語句(OGNL語法)


下面是Mybatis動態sql語句(即OGNL語法)的簡單案例

 

1.創建表作為案例測試使用,剩下的Mybatis框架使用步驟就不寫了,這里直接講動態sql

create table test(id int primary key auto_increment,name varchar(20),job varchar(20),dept varchar(20),sal int) charset=utf8;

insert into test values
(null,'魯班','java','',1456),
(null,'后裔','java','',2440),
(null,'劉禪','c','',3540),
(null,'劉備','python','',4505),
(null,'關羽','python','',1470),
(null,'張飛','c','',2345),
(null,'狄仁傑','java','',3640),
(null,'蘭陵王','c','',4000),
(null,'花木蘭','c','',1000),
(null,'諸葛亮','java','',2047),
(null,'甄姬','python','',3000),
(null,'小喬','c','',4000),
(null,'女蝸','java','',1000),
(null,'妲己','java','',6705),
(null,'公孫策','java','',null),
(null,'百里守約','c','',null),
(null,'小劉','python','',842),
(null,'蔡文姬','python',null,500);
創建 test 表

 

2.<if> 標簽

    <select id="selectTest" resultType="bean.TestBean" parameterType="bean.TestBean">

        select * from test where
            <if test="id!=null">        <!-- test="id!=null" 這里的 id 值是調用了parameterType參數的 getId()方法獲取的 -->
                                        <!-- 所以參數不能是基本類型(因為基本類型和包裝類沒有 get()方法) -->
                id=#{id}                <!-- 如果 id 不等於 null ,這段sql語句變成如下所示--> </if>                       <!-- select * from test where id=#{id} or id=1 -->
                or id=1                 <!-- 如果 id 等於 null(即 參數.getId() == null) ,那么語句這段sql語句變成如下所示 -->
                                        <!-- select * from where or id=1  報錯!,這時候要用 <where> 標簽防止這種情況發生 -->
                
    </select>

 

 

3.<where>標簽

案例1

<select id="selectTest" resultType="bean.TestBean" parameterType="bean.TestBean">
select * from test  <where>            <!-- 使用<where>標簽后,如果where后面緊跟着的是 or 和 and ,那么這兩個關鍵字會被忽視-->   <if test="id!=null">   <!-- 例如id等於null 那么這段代碼變成 select * from where id=1 sql語句正常-->                id=#{id}           </if>    or id=1   </where>
        
</select>


案例2

<select id="selectTest" resultType="bean.TestBean" parameterType="bean.TestBean"> select * from test    <where>                or id=1          <!-- or 被忽視,正常運行 -->   </where>          <!-- 接下來要講的是<trim>標簽,它能定義<where>的過濾規則 --> </select>

 

4.<trim>標簽

    <select id="selectTest" resultType="bean.TestBean"parameterType="bean.TestBean">
        select * from test
        <trim prefix="where" prefixOverrides="and |or |abc "> <!-- 這段代碼的效果個上面的案例2效果一模一樣 -->
            abc id=1                         <!-- prefixOverrides:前綴覆蓋--> </trim>                            <!-- 也就是說,where的后面緊跟着的是 and\or\abc,那么這些關鍵字都會被忽略 -->
    </select>                             <!-- 要注意 | 后面不能有空格,例如: |a 和| a 后面這個a和|之間有空格,會導致忽略失敗 -->

 

5.<set>標簽

案例1

<update id="updateTest" parameterType="bean.TestBean">
update test set <if test="name!=null"> name=#{name}, </if> <if test="sal!=null"> sal=#{sal} </if> where id=#{id}
</update>


<!-- 上面這段代碼,還沒有使用<set>標簽,這段代碼有個漏洞,如果 name!=null ,且 sal==null ,那么語句變成-->
<!-- update test set name=#{name}, where id=#{id} -->
<!-- 這里的逗號跟着寫進來了,程序理所當然的報錯,這時候我們就要用到<set>標簽解決這個問題 --
案例2

<update id="updateTest" parameterType="bean.TestBean"> update test <set> <if test="name!=null"> name=#{name}, </if> <if test="sal!=null"> sal=#{sal} </if> </set> where id=#{id} </update>

<!-- <set>標簽和<where>標簽有點相反的意思,<set>標簽定義了會忽視最后的逗號 “,” 例如<set> name=#{name},</set>里面,最后的是“,”結尾,所以被忽視了,程序正常運行-->

<!-- 當然,<trim>標簽同樣可以定義<set>標簽的規則,下面案例可以看到 -->
案例3

<update id="updateTest" parameterType="bean.TestBean"> update test <trim prefix="SET" suffixOverrides=", |abc"> <if test="name!=null"> name=#{name}, </if> <if test="sal!=null"> sal=#{sal}abc </if> </trim> where id=#{id} </update>

<!-- 使用<trim>定義<set>規則 -->
<!-- suffixOverrides=", |abc",定義了無論是逗號","結尾還是"abc"結尾,都會被程序忽視,上面程序正常運行 -->
<!-- 文中的abc規則是我添加的,原本只有過濾逗號"," -->

 


免責聲明!

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



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