目錄
動態SQL
- 根據不同的條件執行不同的SQL命令,稱爲動態SQL
- 在Mybatis中的Mapper.xml中添加邏輯判斷
符號
在Mybatis中,運算符號會被轉義成字節碼,所以要用代碼符號
< | <= | > | >= | & | ' | " |
< | <= | > | >= | & | ' | " |
if標籤(邏輯判斷)
- 成立則執行,不成立則不執行
<select id="selByAccinAccout" resultType="log">
select * from log where 1=1
<!-- OGL表達式,直接寫key或對象的屬性,不需要添加任何特殊字符號 -->
<if test="accin!=null and accin!=''">
and accin = #{accin}
</if>
<if test="accout!=null and accout!=''">
and accout = #{accout}
</if>
</select>
where標籤(SQL判斷)
- 當編寫where標籤時,如果內容中第一個是and去掉第一個and
- 如果 <where>中有內容會生成where關鍵字,如果沒有內容不生成where關鍵字
<select id="selByAccinAccout" resultType="log">
select * from log
<where>
<if test="accin!=null and accin!=''">
and accin = #{accin}
</if>
<if test="accout!=null and accout!=''">
and accout = #{accout}
</if>
</where>
</select>
choose,when,otherwise(Java中的switch)
- 只要有一個成立,其他都不執行
- 如果title和content都不爲null或都不爲""
- 生成的sql中只有where title=?
- 如果title和content都爲null或都爲""
- 生成的sql中只有where owner = “owner1”
<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">
select * from t_blog where 1=1
<choose>
<when test="title != null">
and title = #{title}
</when>
<when test="content != null">
and content = #{content}
</when>
<!-- <otherwise>可以不寫 -->
<otherwise>
and owner = "owner1"
</otherwise>
</choose>
</select>
set (sql修改)
- 作用:去掉最後一個逗號
- 作用:如果<set>裏面有內容就會生成set關鍵字,沒有就不生成
<update id="upd" parameterType="log">
update log
<set>
<!-- 防止不生成set關鍵字造成語法錯誤 -->
id=#{id},
<if test="accIn!=null and accIn!=''">
accIn = #{accIn},
</if>
<if test="accOut!=null and accOut!=''">
accOut = #{accOut},
</if>
</set>
where id=#{id}
</update>
trim(截斷 添加)
- prefix 在前面添加內容
- suffix 在後面添加內容
- prefixOverrides 去掉前面內容
- suffixOverrides 去掉後面內容
<update id="upd" parameterType="log">
update log
<!-- 去掉了後面的內容 -->
<!-- 覆蓋了標籤後的逗號 -->
<!-- 適用於存在符號和關鍵字的參數(例如金錢符號$) -->
<trim prefix="set" suffixOverrides>
a=a,
</trim>
where id=100
</update>
bind(模糊查詢)
- 作用:給參數重新賦值
- 場景:模糊查詢 | 在原內容前或後添加內容
<select id="selByLog" parameterType="log" resultType="log">
select * from log
<where>
<!-- 常用語模糊查詢(添加%) -->
<if test="title!=null and title!=''">
<bind name="title" value="'$'+title+'$'"/>
and title like #{title}
</if>
<!-- bind:給參數附加字符串 -->
<if test="money!=null and money!=''">
<bind name="money" value="'$'+money"/>
and money = #{money}
</if>
</where>
</select>
foreach(循環)
- 循環參數內容,還具備在內容的前後添加內容,還具備添加分割符功能
- 適用場景:in查詢 | 批量新增(mybatis中foreach效率大幅度降低)
- 如果希望批量新增,SQL命令
- openSession()必須指定
- //底層的JBDC的PrepareStatement.addBatch()
- factpry.openSession(ExecuteorType.BATCH);
- openSession()必須指定
- 如果希望批量新增,SQL命令
- 屬性
- collection:添加要遍歷的集合
- item:迭代變量,循環內使用#{迭代變量名}來獲取內容
- open:循環後左側添加的內容
- close:循環後右側添加的內容
- separator:添加每次遍歷尾部追加的分割符
<select id="selIn" parameterType="list" resultType="log">
select * from log where id in
<foreach collection="list" item="a" open="(" close=")" separator=",">
#{a}
</foreach>
</select>
sql (複用)
- 某些SQL片段如果需要複用,可以使用<sql>這個標籤
<sql id="mysql">
id,accin,accout,money
</sql>
- 在<select>或<upfate>或<insert>中使用<include>標籤進行復用引用
<select id="">
select <include refid="mysql"></include> from log
</select>