-
本文只介紹動態(tài)mybatis動態(tài)sql的使用
-
if元素------------------------------------
<! -- id:跟持久層對應的方法名稱 parameterType:持久層傳來數(shù)據(jù)的類型 resultType:返回的數(shù)據(jù)類型-->
<select id="getByName" parameterType="com.qinglin.entity.Yang" resultType="com.qinglin.entity.Yang">
select id,name,age from yang as y
<where>
<! --判斷在業(yè)務層傳來的數(shù)據(jù)是否為空-->
<if test="name!=null">
<! -- #{這里面寫的是從業(yè)務層傳來的屬性的名稱}-->
y.name=#{name}
</if>
<if test="id!=null">
and y.id=#{id}
</if>
</where>
</select>
-
where元素(自動拼接關鍵字)------------------------------------
<! -- id:跟持久層對應的方法名稱 parameterType:持久層傳來數(shù)據(jù)的類型 resultType:返回的數(shù)據(jù)類型-->
<select id="getByName" parameterType="com.qinglin.entity.Yang" resultType="com.qinglin.entity.Yang">
select id,name,age from yang as y
<where>
<! --判斷在業(yè)務層傳來的數(shù)據(jù)是否為空-->
<if test="name!=null">
<! -- #{這里面寫的是從業(yè)務層傳來的屬性的名稱}-->
y.name=#{name}
</if>
<if test="id!=null">
and y.id=#{id}
</if>
</where>
</select>
-
choose元素------------------------------------
-
注意:when判斷不能用中文判斷,只能用英文跟數(shù)字
<! -- id:跟持久層對應的方法名稱 parameterType:持久層傳來數(shù)據(jù)的類型 resultType:返回的數(shù)據(jù)類型-->
<select id="getByAge" parameterType="com.qinglin.entity.Yang" resultMap="yangPojo">
select * from yang as y
<where>
<! -- 在choose標簽中的結果只能有一條 -->
<choose>
<! -- 如果第一個條件成立,就結束choose判斷 -->
<when test="name!=null">
y.name=#{name}
</when>
<when test="age!=null">
and y.age=#{age}
</when>
<otherwise>
y.age=20
</otherwise>
</choose>
</where>
</select>
-
set元素------------------------------------
<!-- id:跟持久層對應的方法名稱 parameterType:持久層傳來數(shù)據(jù)的類型 -->
<update id="updateUser_if_set" parameterType="com.edu.domain.User">
UPDATE user
<! -- 使用set標簽表明接下來的sql拼接得事修改的sql -->
<set>
<if test="username!= null and username != '' ">
username = #{username},
</if>
<if test="sex!= null and sex!= '' ">
sex = #{sex},
</if>
<if test="birthday != null ">
birthday = #{birthday},
</if>
</set>
WHERE id = #{id};
</update>
-
trim元素------------------------------------
-
? trim 是更靈活用來去處多余關鍵字的標簽,它可以用來實現(xiàn) where 和 set 的效果。
-
trim代替where
<!-- 使用 if/trim 代替 where(判斷參數(shù)) - 將 User 類不為空的屬性作為 where 條件 -->
<select id="getUsertList_if_trim" resultMap="userResultMap">
SELECT *
FROM user u
<! -- prefix:代替where , prefixOverrides:覆蓋前綴的名稱, | 是或者的意思 -->
<trim prefix="WHERE" prefixOverrides="AND|OR">
<if test="username !=null ">
u.username=#{username}
</if>
<if test="sex != null and sex != '' ">
AND u.sex = #{sex}
</if>
<if test="birthday != null ">
AND u.birthday = #{birthday}
</if>
</trim>
</select>
<!-- if/trim代替set(判斷參數(shù)) - 將 User 類不為空的屬性更新 -->
<update id="updateUser_if_trim" parameterType="com.qf.pojo.User">
UPDATE user
<! -- prefix:代替set,prefixOverrides:覆蓋后綴的名稱,如果sql語句最后面多了個逗號,就將此逗號刪除 -->
<trim prefix="SET" suffixOverrides=",">
<if test="username != null and username != '' ">
username = #{username},
</if>
<if test="sex != null and sex != '' ">
sex = #{sex},
</if>
<if test="birthday != null ">
birthday = #{birthday},
</if>
</trim>
WHERE user_id = #{user_id}
</update>
-
foreach元素------------------------------------
-
單參數(shù)List的類型
<select id="dynamicForeachTest" resultType="user">
select * from user where id in
<! -- collection:需要迭代的數(shù)據(jù)的類型,index:迭代每個數(shù)據(jù)的下標,item:迭代每個元素的元素本身,
open:循環(huán)開始的位置,separator:每個元素之間的分隔符,close:循環(huán)結束的位置-->
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
<select id="dynamicForeach2Test" resultType="user">
select * from user where id in
<! -- collection:array表示的是數(shù)組 -->
<foreach collection="array" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
<select id="dynamicForeach3Test" resultType="user">
select * from user where username like "%"#{username}"%" and id in
<! -- 如果有多中不同的類型,使用以下這種形式,ids這個名稱是我從業(yè)務層傳來的對象的類型,
比如我傳過來的是有個map集合,ids寫的就是map集合的key -->
<foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>