- 關於spring整合mybatis的步驟applicationContext.xml的配置:https://blog.csdn.net/IT_CREATE/article/details/85329007
- mybatis框架之mapper的xml映射配置的編寫(insert、delete、update、select標籤使用):https://blog.csdn.net/IT_CREATE/article/details/85687293
- mybtis中的級聯關係(關聯關係、集合關係)association、collection(一對一關係、一對多關係、多對多關係):https://blog.csdn.net/IT_CREATE/article/details/86523101
- mybatis中的級聯關係中的鑑別器(discriminator、case、result):https://blog.csdn.net/IT_CREATE/article/details/86561051
動態sql需要使用的標籤
在mybatis中,我們除了做一些固定sql之外,還要做一些動態的sql拼接,因爲我們要對從瀏覽器傳下來的參數進行值判斷,爲不爲空這些啊,所以我們根據條件去做到動態sql拼接,才滿足我們的需求。
在mybitis中的sql語句都是進行拼接的,意思就是任何用於動態sql的標籤都是在前面已經寫的sql語句後面進行追加sql語句。
邏輯標籤的使用
1、<where>--<if>標籤
if標籤,顧名思義,就和我們Java中的if一樣,是用做判斷的,if標籤可單獨使用,也可嵌套where標籤使用。它的使用也很簡單。
比如我做查找時:(test屬性就是寫條件表達式的)
<select id="findUserBeanByMap" resultMap="userMap">
select
id,login_name,user_name,age,gender,user_pwd,birthday,create_time from
t_user
<where>
1 = 1
<if test="m.userName != null && m.userName != ''">
and user_name like concat(#{m.userName},'%')
</if>
<if test="m.age != null">
and age = #{m.age}
</if>
<if test="m.gender != null">
and gender = #{m.gender}
</if>
</where>
</select>
裏面有where標籤,這個標籤就是自動在後面拼接where的,用於我們拼接where條件,在where標籤中,如果第一個條件前面含有“AND”或“OR”,它會自動進行忽略,一般和if標籤進行嵌套使用,用於動態拼接where條件。同時注意的是:在xml中有時我們用一些“<”這類符號去做if條件判斷時,可能它會認不到,所以我們要用到轉義字符,常用的有:
< < 小於號
> > 大於號
& & 和
' ' 單引號
" " 雙引號
實體必須以符號"&"開頭,以符號";"結尾。 注意: 只有"<" 字符和"&"字符對於XML來說是嚴格禁止使用的。剩下的都是合法的,爲了減少出錯,使用實體是一個好習慣。
其實標籤之間是可以嵌套使用,也可以單獨使用,根據你的情況而定。
2、<foreach>標籤
顧名思義,就是用作循環用的,相當於java中的foreach循環。
比如我們做批量新增時:(collection表示的是集合,只就是從接口傳下來的集合;item表示的是集合每個單獨的值,自己隨便起名字;separator表示每次循環結束用什麼隔開)
<insert id="addBatchUserBean">
<!-- 批量新增的語法:insert into 表 (列名列表) values (),(),()…… -->
insert into
t_user(login_name,user_name,user_pwd,age,gender,birthday,create_time)
values
<!-- 循環語句,類似我們java的 for(UserBean u,users){ } ,separator表示每個結果之間用什麼隔開-->
<foreach collection="users" item="u" separator=",">
(#{u.loginName},#{u.userName},#{u.password},#{u.age},#{u.gender},#{u.birthday},now())
</foreach>
</insert>
比如我們用於批量刪除時:(foreach 該標記主要用於遍歷集合或者數組,collection 屬性代表需要遍歷的集合或數組,item 代表每次遍歷取出來的數據,open 代表遍歷開始前,以什麼符號開始, close 代表遍歷結束後,以什麼符號結束 ,separator 代表每次遍歷的分隔符):
<delete id="deleteBatchUserBean">
<!-- 批量刪除的語法:delete from 表 where id in (……) -->
<!-- collection 如果接口傳下來的list參數並未取參數別名,那麼此處就要寫成array,
取了別名,那麼就按照別名來寫,例如:ids -->
delete from t_user where id in
<foreach collection="ids" item="id" open="(" close=")"
separator=",">
#{id}
</foreach>
<!-- foreach 該標記主要用於遍歷集合或者數組,collection 屬性代表需要遍歷的
集合或數組 item 代表每次遍歷取出來的數據,open 代表遍歷開始前,以什麼符號開始,
close 代表遍歷結束後,以什麼符號結束 ,separator 代表每次遍歷的分隔符 -->
</delete>
3、<set>--<if>標籤
set標籤主要用在做修改的時候,我們要實現按需修改,就是隻對錶中的特定列進行修改的時候用,它主要和if標籤嵌套使用。
這裏,set 元素會動態前置 SET 關鍵字,同時也會刪掉無關的逗號,因爲用了條件語句之後很可能就會在生成的 SQL 語句的後面留下這些逗號。(譯者注:因爲用的是“if”元素,若最後一個“if”沒有匹配上而前面的匹配上,SQL 語句的最後就會有一個逗號遺留),意思是如果在set中最後的條件沒匹配上,會遺留下前面匹配上的條件的“,”,所以爲了避免這種情況最後的條件一定要匹配上,所以我們可以在最後加上一個一定能匹配上的條件。
比如:
<update id="updateUserBean">
update t_user
<!-- set-if判斷,可以在修改的時候對指定列進行修改,而不是全部修改-->
<set>
<if test="u.loginName != null and u.loginName != ''">
login_name = #{u.loginName},
</if>
<if test="u.userName != null and u.userName != ''">
user_name = #{u.userName},
</if>
<if test="u.age != null">
age = #{u.age},
</if>
<if test="u.gender != null">
gender = #{u.gender},
</if>
<if test="u.password != null and u.password != ''">
user_pwd = #{u.password},
</if>
<if test="u.birthday != null">
birthday = #{u.birthday},
</if>
update_time = now()
</set>
where id = #{u.id};
</update>
4、<choose>--<when>--<otherwise>標籤
這一組標籤等同於java中的switch case default條件控制語句,用法如下,不過比起比起上面的where--if嵌套,明顯where--if要更好一些,where--if足以。
<select id="findUserBeanByObject" resultMap="userMap">
select
id,login_name,user_name,age,gender,user_pwd,birthday,create_time from
t_user
<where>
<!-- choose when otherwise 等同於java中的switch case default -->
<choose>
<when test="u.userName != null && u.userName != ''">
and user_name like concat(#{u.userName},'%')
</when>
<when test="u.age != null">
and age = #{u.age}
</when>
<when test="u.gender != null">
and gender = #{u.gender}
</when>
<otherwise>
1=1
</otherwise>
</choose>
</where>
order by ${sort} ${order}-->
</select>
5、<trim>--<if>標籤
這個標籤其實和where--if標籤非常接近,也是動態拼接,不過使用where--if就足夠了。
<select id="findUserBeanByObject" resultMap="userMap">
select
id,login_name,user_name,age,gender,user_pwd,birthday,create_time from
t_user
<!-- trim if 作用跟where if 非常的接近
prefix="where" 表示需要條件判斷之前,以什麼開始
prefixOverrides="and|or" 如果where 後直接跟and或者or ,那麼將使用空字符串替換-->
<trim prefix="where" prefixOverrides="and|or">
<if test="u.userName != null && u.userName != ''">
and user_name like concat(#{u.userName},'%')
</if>
<if test="u.age != null">
and age = #{u.age}
</if>
<if test="u.gender != null">
and gender = #{u.gender}
</if>
</trim>
order by ${sort} ${order}
</select>
以上這些標籤足夠了,也是我們常用的。
更多動態sql標籤參考myvatis官網:http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html