點擊這裏 查看Mybatis動態SQL官方文檔
對於初學者來說查看文檔內容有好多細節不理解這裏我結合我自身學習動態SQL實際遇到的困惑以及細節標示做一個梳理
首先幾個常用的關鍵字:
- if 判斷,當條件符合則進入條件語句(#{id}代表傳入的值)
<!-- 如果條件都滿足 則sql爲 select * from test_table where id=? and last_name=?
如果id==null則sql爲 select * from test_table where and last_name=? sql就會報錯
兩種處理方案 1.
我們使用 select * from test_table where 1=1 後面所有的if 前面都加 and
select * from test_table where 1=1
<if test="id!=null">
and id=#{id}
</if>
<if test="lastName!=null">
and last_name=#{lastName},
</if>
2.使用where標籤 <where></where> 後面單獨講
-->
select * from test_table where
<if test="id!=null">
id=#{id}
</if>
<if test="lastName!=null">
and last_name=#{lastName},
</if>
- choose (when, otherwise):分支選擇;類似於我們的swtich-case break語句,只會進入一個選擇條件
<!-- public List<TestBean> getInfo(TestBean testbean); -->
<select id="getInfo" resultType="com.xiaoqiang.mybatis.bean.TestBean">
select * from test_table
<where>
<!-- 如果帶了id就用id查,如果帶了lastName就用lastName查;只會進入其中一個 -->
<choose>
<when test="id!=null">
id=#{id}
</when>
<when test="lastName!=null">
last_name like #{lastName}
</when>
<when test="email!=null">
email = #{email}
</when>
<otherwise>
1=1
</otherwise>
</choose>
</where>
</select>
- trim 字符串截取(where(封裝查詢條件), set(封裝修改條件))
<!-- 使用where標籤
如果拼接第一個條件的時候第一個條件不成立則sql就會報錯,
例如下面代碼的id==null則不會進入id=#{id} sql就變成:select * from table where and email=? 明顯sql報錯
使用where標籤就會把前面的and去掉,保證sql正常。
但是如果我們把and加到語句後面則where標籤就無法實現了只能使用 trim 標籤來處理,一般沒這麼寫的
<if test="id!=null">
id=#{id} and
</if>
-->
<where>
<if test="id!=null">
id=#{id}
</if>
<if test="email!=null">
and email=#{email}
</if>
</where>
<!-- 使用set 是爲了如果拼到最後的還有逗號,就會把最後的逗號去掉 我們仍然可以使用 trim 標籤來替代set標籤-->
<update id="updateBean">
<!-- Set標籤的使用 -->
update test_table
<set>
<if test="lastName!=null">
last_name=#{lastName},
</if>
<if test="email!=null">
email=#{email},
</if>
</set>
where id=#{id}
</update>
<!--我們使用trim標籤替代where標籤來定製where標籤功能-->
<select id="getBean" resultType="com.xiaoqiang.mybatis.bean.TestBean">
select * from test_table
<!-- 後面多出的and或者or where標籤不能解決的問題
prefix="":前綴:trim標籤體中是整個字符串拼串 後的結果。
prefix給拼串後的整個字符串加一個前綴
prefixOverrides="":
前綴覆蓋: 去掉整個字符串前面多餘的字符
suffix="":後綴
suffix給拼串後的整個字符串加一個後綴
suffixOverrides=""
後綴覆蓋:去掉整個字符串後面多餘的字符
-->
<!-- 自定義字符串的截取規則 suffixOverrides處理後綴and -->
<trim prefix="where" suffixOverrides="and">
<if test="id!=null">
id=#{id} and
</if>
<if test="lastName!=null">
last_name like #{lastName} and
</if>
<if test="email!=null">
email=#{email} and
</if>
</trim>
</select>
<!--我們使用trim標籤替代set標籤 suffixOverrides處理後綴逗號-->
update test_table
<trim prefix="set" suffixOverrides=",">
<if test="lastName!=null">
last_name=#{lastName},
</if>
<if test="email!=null">
email=#{email},
</if>
</trim>
where id=#{id}
- foreach 遍歷集合
<!--public List<TestBean> getBeans(List<Integer> ids); -->
<select id="getBeans" resultType="com.xiaoqiang.mybatis.bean.TestBean">
select * from test_table where id in
<!--
sql : select * from test_table where id in (1,2,3)
collection:指定要遍歷的集合
item:將當前遍歷出的元素賦值給指定的變量
separator:每個元素之間的分隔符
open:遍歷出所有結果拼接一個開始的字符
close:遍歷出所有結果拼接一個結束的字符
index:索引。遍歷list的時候是index就是索引,item就是當前值
遍歷map的時候index表示的就是map的key,item就是map的值
使用#{變量名}就能取出變量的值也就是當前遍歷出的元素
-->
<foreach collection="ids" item="item_id" separator=","
open="(" close=")">
#{item_id}
</foreach>
</select>
- 抽取重複片段使用:
<!--
抽取可重用的sql片段。方便後面引用
1、sql抽取:經常將要查詢的列名,或者插入用的列名抽取出來方便引用
2、include來引用已經抽取的sql
-->
<sql id="insertColumn">
last_name,email
</sql>
<!-- 使用include 重用抽取出來的sql片段 -->
insert into test_table (
<include refid="insertColumn"></include>
)
values('name','[email protected]')