- 動態(tài) SQL
- if 條件判斷
```xml
<select id="findActiveBlogLike" resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</select>
```
- choose (when, otherwise)條件查詢
```xml
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<choose>
<when test="title != null">
AND title like #{title}
</when>
<when test="author != null and author.name != null">
AND author_name like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
```
- trim (where, set)
- where:元素只會在至少有一個子元素的條件返回 SQL 子句的情況下才去插入“WHERE”子句。而且颗胡,若語句的開頭為“AND”或“OR”锁施,where 元素也會將它們?nèi)コ?
```xml
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG
<where>
<if test="state != null">
state = #{state}
</if>
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</where>
</select>
```
- trim:通過自定義 trim 元素來定制 where 元素的功能
```xml
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
```
- set:元素可以用于動態(tài)包含需要更新的列寄雀,而舍去其它的
```xml
<update id="updateAuthorIfNecessary">
update Author
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio}</if>
</set>
where id=#{id}
</update>
```
- foreach:對一個集合進行遍歷秧均,通常是在構(gòu)建 IN 條件語句的時候
```xml
<select id="selectPostIn" resultType="domain.blog.Post">
SELECT *
FROM POST P
WHERE ID in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
```
- bind:元素可以從 OGNL 表達式中創(chuàng)建一個變量并將其綁定到上下文
```xml
<select id="selectBlogsLike" resultType="Blog">
<bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
SELECT * FROM BLOG
WHERE title LIKE #{pattern}
</select>
```
mybatis入門
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來爵卒,“玉大人虚缎,你說我怎么就攤上這事〉鲋辏” “怎么了实牡?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長享幽。 經(jīng)常有香客問我铲掐,道長,這世上最難降的妖魔是什么值桩? 我笑而不...
- 正文 為了忘掉前任摆霉,我火速辦了婚禮,結(jié)果婚禮上奔坟,老公的妹妹穿的比我還像新娘携栋。我一直安慰自己,他們只是感情好咳秉,可當我...
- 文/花漫 我一把揭開白布婉支。 她就那樣靜靜地躺著,像睡著了一般澜建。 火紅的嫁衣襯著肌膚如雪向挖。 梳的紋絲不亂的頭發(fā)上蝌以,一...
- 文/蒼蘭香墨 我猛地睜開眼蒜危,長吁一口氣:“原來是場噩夢啊……” “哼虱痕!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起辐赞,我...
- 正文 年R本政府宣布位谋,位于F島的核電站山析,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏掏父。R本人自食惡果不足惜笋轨,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望赊淑。 院中可真熱鬧爵政,春花似錦、人聲如沸陶缺。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽饱岸。三九已至掺出,卻和暖如春徽千,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背汤锨。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 最近兩年 springmvc + mybatis 的在這種搭配還是蠻火的,樓主我呢聂抢,也從來沒真正去接觸過mybat...
- 由于之前我們已經(jīng)有了hibernate的基礎琳疏,所以這里很多細節(jié)就不再提及有决。 一、基本架構(gòu) 這里從網(wǎng)絡上找了幾張my...