3. Mybatis入門,動態sql,if條件,choose,trim,foreach循環,bind

if:條件判斷語句,單條件

choose(when,otherwise) 相當於case when

trim(set,where):輔助語句,主要用來處理一些sql拼裝

foreach:循環語句

if條件語句

<select id=”findActiveBlogWithTitleLike”parameterType=”Blog” resultType=”Blog”>
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<if test=”title != null”>
AND title like #{title}
</if>
</select>

如果滿足if條件,則會拼接對應的sql

<select id=”findActiveBlogLike”
parameterType=”Blog” 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 title like #{author.name}
	</when>
	<otherwise>
		AND featured = 1
	</otherwise>
</choose>
</select>

where條件

<select id=”findActiveBlogLike” parameterType=”Blog” 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 title like #{author.name}
	</if>
</where>
</select>

這兩者效果一樣。
在使用if條件的時候,如果使用where條件,如果前面沒有條件,我們可能需要使用where 1=1然後在if中使用and xxx這種格式,但是使用<where>條件的時候不用,mybatis默認會給<where>條件增加where的sql,同時條件的第一個and語句會自動去掉。這樣就不用被動的寫 where 1=1這種了

trim主要是用來去除某些SQL語法的時候使用,比如上面的<where>條件

<select id=”findActiveBlogLike” parameterType=”Blog” resultType=”Blog”>
SELECT * FROM BLOG
< trim prefix= "WHERE" prefixOverrides= "AND |OR " > 
<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 title like #{author.name}
</if>
</ trim > 
</select>

如果當state爲空的時候,正常情況下sql爲:
SELECT * FROM BLOG where AND title like #{title} AND title like #{author.name}

但是trim會把where前面的AND或者OR關鍵字去除,變爲:
SELECT * FROM BLOG where title like #{title} AND title like #{author.name}

update user
  <trim prefix="set" suffixoverride="," suffix=" where id = #{id} ">
    <if test="name != null and name.length()>0"> name=#{name} , </if>
    <if test="gender != null and gender.length()>0"> gender=#{gender} ,  </if>
  </trim>

如說name和gender的值都不爲null的話打印的SQL爲:update user set name=‘xx’ , gender=‘xx’ where id=‘x’
在where前面不存在逗號,而且自動加了一個set前綴和where後綴,上面三個屬性的意義如下,其中prefix意義如上:
suffixoverride:去掉最後一個逗號(也可以是其他的標記,就像是上面前綴中的and一樣)
suffix:後綴

set元素主要是用來解決當滿足一個條件時,需要用where語句,而不滿足時,不需要
在update中類似的,用set元素:

<update id="updateAuthorIfNecessary" parameterType="domain.blog.Author">
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元素:

<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>

open和close表示用什麼符號把元素包裝起來,separator是各個元素的間隔符
item是循環中當前的元素

bind元素

bind元素聽過OGNL表達式定義一個上下文的變量:

<select id=”findActiveBlogWithTitleLike”parameterType=”Blog” resultType=”Blog”>
	<bind name="pattern_title" value="'%'+title+'%'" />
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
and title like #{pattern_title}
</select>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章