<select>、<insert>、<update>、<delete>
<trim>:trim標簽主要就是標記的作用,可以去掉if條件不滿足時多余的and或者or或者,等等,和set標簽或者where標簽相通。
trim標記是一個格式化的標記,可以完成set或者是where標記的功能,如下代碼:
1、
select * from user
<trim prefix="WHERE" prefixoverride="AND |OR">
<if test="name != null and name.length()>0"> AND name=#{name}</if>
<if test="gender != null and gender.length()>0"> AND gender=#{gender}</if>
</trim>
假如說name和gender的值都不為null的話打印的SQL為:select * from user where name = 'xx' and gender = 'xx'
在紅色標記的地方是不存在第一個and的,上面兩個屬性的意思如下:
prefix:前綴
prefixoverride:去掉第一個and或者是or
2、
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'
在紅色標記的地方不存在逗號,而且自動加了一個set前綴和where后綴,上面三個屬性的意義如下,其中prefix意義如上:
suffixoverride:去掉最后一個逗號(也可以是其他的標記,就像是上面前綴中的and一樣)
suffix:后綴
<where>:where是sql語句中的關鍵字,把他作為標簽是防止sql語句中where之后的判斷語句不成立,多出where關鍵字,使得sql語句出錯。
<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>
</select>
<set>:使用set標簽可以將動態的配置SET 關鍵字,和剔除追加到條件末尾(if語句中)的任何不相關的逗號。
<update id="updateByPrimaryKeySelective" parameterType="RecruitmentConfBanner">
UPDATE conf_banner t
<set>
<if test="bannerName != null">
t.banner_name = #{bannerName},
</if>
<if test="bannerUrl != null">
t.banner_url = #{bannerUrl},
</if>
<if test="bannerLogo != null">
t.banner_logo = #{bannerLogo},
</if>
<if test="bannerDescription != null">
t.banner_description = #{bannerDescription},
</if>
<if test="sort != null">
t.sort = #{sort},
</if>
<if test="isEnabled != null">
t.is_enabled = #{isEnabled},
</if>
</set>
where t.banner_id = #{bannerId}
</update>
mybatis接受的參數分為:(1)基本類型(2)對象(3)List(4)數組(5)Map
無論傳哪種參數給mybatis,他都會將參數放在一個Map中:
如果傳入基本類型:變量名作為key,變量值作為value 此時生成的map只有一個元素。
如果傳入對象: 對象的屬性名作為key,屬性值作為value,
如果傳入List: "list"作為key,這個List是value (這類參數可以迭代,利用<foreach>標簽實現循環)
如果傳入數組: "array"作為key,數組作為value(同上)
如果傳入Map: 鍵值不變。
<foreach>:標簽有循環的功能,可以用來生成有規律的SQL語句,主要屬性有:
item:表示集合每一個元素進行迭代時的別名
index:表示在迭代過程中,每次迭代到的位置
open:表示該語句已什么開始
separator:表示每次迭代之間以什么符號作為分隔
close:表示該語句已什么結束
collection:需要迭代的變量
<update id="pubS" parameterType="Map">
UPDATE BMC_SUBPLATE
SET PLSTATUS = '02'
WHERE
<foreach collection="ids" item="plid" open="" close="" separator="OR">
PLID = #{plid}
</foreach>
</update>
collection的值其實就是mybatis把參數轉化成Map以后,這個Map的key,但是這個key對應的value必須是一個集合, 可以是數組,也可以是List
生成的動態sql:
Executing: UPDATE BMC_SUBPLATE SET PLSTATUS = '02' WHERE PLID = ? OR PLID = ?
Parameters: 20121116144947537692(String), 20121116141301414855(String)
<if>:查詢標簽
<choose>:查詢標簽,choose(when,otherwise)相當于java中的switch/case
if和choose酌情使用
<bind>:可以從 OGNL 表達式中創建一個變量并將其綁定到上下文。比如:
<select id="selectBlogsLike" resultType="Blog">
<bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />
SELECT * FROM BLOG
WHERE title LIKE #{pattern}
</select>
下面是映射xml文件中的其他一些標簽的用法:
<parameterMap>
<include>:標簽引入sql片段,通過<sql>的id值,引用一些可以重復使用的代碼段。
<resultMap>:將sql標簽執行的結果返回,逐一定義列名和對象屬性名之間的映射關系。
<selectKey>:在不支持自增的主鍵而生成的策略標簽。
SelectKey在Mybatis中是為了解決Insert數據時不支持主鍵自動生成的問題,他可以很隨意的設置生成主鍵的方式。
不管SelectKey有多好,盡量不要遇到這種情況吧,畢竟很麻煩。
selectKey Attributes
屬性 描述
keyProperty selectKey 語句結果應該被設置的目標屬性。
resultType 結果的類型。MyBatis 通常可以算出來,但是寫上也沒有問題。MyBatis 允許任何簡單類型用作主鍵的類型,包括字符串。
order 這可以被設置為 BEFORE 或 AFTER。如果設置為 BEFORE,那么它會首先選擇主鍵,設置 keyProperty 然后執行插入語句。如果設置為 AFTER,那么先執行插入語句,然后是 selectKey 元素-這和如 Oracle 數據庫相似,可以在插入語句中嵌入序列調用。
statementType 和前面的相 同,MyBatis 支持 STATEMENT ,PREPARED 和CALLABLE 語句的映射類型,分別代表 PreparedStatement 和CallableStatement 類型。
SelectKey需要注意order屬性,像Mysql一類支持自動增長類型的數據庫中,order需要設置為after才會取到正確的值。
像Oracle這樣取序列的情況,需要設置為before,否則會報錯。
下面是一個xml和注解的例子,SelectKey很簡單,兩個例子就夠了
<insert id="insert" parameterType="map">
insert into table1 (name) values (#{name})
<selectKey resultType="java.lang.Integer" keyProperty="id">
CALL IDENTITY()
</selectKey>
</insert>
上面xml的傳入參數是map,selectKey會將結果放到入參數map中。用POJO的情況一樣,但是有一點需要注意的是,keyProperty對應的字段在POJO中必須有相應的setter方法,setter的參數類型還要一致,否則會報錯。
@Insert("insert into table2 (name) values(#{name})")
@SelectKey(statement="call identity()", keyProperty="nameId", before=false, resultType=int.class)
int insertTable2(Name name);