下面是Mybatis動態sql語句(即OGNL語法)的簡單案例java
1.建立表做爲案例測試使用,剩下的Mybatis框架使用步驟就不寫了,這裏直接講動態sqlpython
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);
2.<if> 標籤sql
<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>標籤ide
<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規則是我添加的,本來只有過濾逗號"," -->