3.3.10 动态SQL
?
十、動(dòng)態(tài)SQL
根據(jù)條件的不同, SQL 語句也會(huì)隨之動(dòng)態(tài)的改變. MyBatis 中,提供了一組標(biāo)簽用于實(shí)現(xiàn)動(dòng)態(tài) SQL.
1. <if>
用于進(jìn)行條件判斷, test 屬性用于指定判斷條件. 為了拼接條件, 在 SQL 語句后強(qiáng)行添加 1=1 的恒成立條件.
<select?id="sel"?resultType="user">
select?*?from?t_user where?1=1
<if?test="username != null and username != ''">
and?username=#{username}
</if>
<if?test="password != null and password != ''">
and?password=#{password}
</if>
</select>
?
2. <where>
用于管理 where 子句. 有如下功能:
(1) 如果沒有條件, 不會(huì)生成 where 關(guān)鍵字
(2) 如果有條件, 會(huì)自動(dòng)添加 where 關(guān)鍵字
(3) 如果第一個(gè)條件中有 and, 去除之
<select?id="sel"?resultType="user">
select?*?from?t_user
<where>
<if?test="username != null and username != ''">
and?username=#{username}
</if>
<if?test="password != null and password != ''">
and?password=#{password}
</if>
</where>
</select>
?
3. <choose><when><otherwise>
這是一套標(biāo)簽, 功能類似于 switch...case...
<select?id="sel"?resultType="user">
select?*?from?t_user
<where>
<choose>
<when?test="username != null and username != ''">
and?username =?#{username}
</when>
<when?test="password != null and password != ''">
and?password =?#{password}
</when>
<otherwise>
and?1=1
</otherwise>
</choose>
</where>
</select>
?
4. <set>
用于維護(hù) update 語句中的 set 子句. 功能如下:
(1) 滿足條件時(shí), 會(huì)自動(dòng)添加 set 關(guān)鍵字
(2) 會(huì)去除 set 子句中多余的逗號(hào)
(3) 不滿足條件時(shí), 不會(huì)生成 set 關(guān)鍵字
int?updUser(User?user);
<update?id="updUser"?parameterType="user">
update?t_user
<set>
id=#{id},?<!-- 防止所有條件不成立時(shí)的語法錯(cuò)誤 -->
<if?test="username != null and username != ''">
username=#{username},
</if>
<if?test="password != null and password != ''">
password=#{password},
</if>
</set>
where?id=#{id}
</update>
?
?
5. <trim>
用于在前后添加或刪除一些內(nèi)容
(1) prefix, 在前面添加內(nèi)容
(2) prefixOverrides, 從前面去除內(nèi)容
(3) suffix, 向后面添加內(nèi)容
(4) suffixOverrides, 從后面去除內(nèi)容
<update?id="updUser"?parameterType="user">
update?t_user
<!--
prefix:?前綴,?表示向前面添加內(nèi)容
prefixOverrides:?從前面刪除內(nèi)容
suffix:?后綴,?表示向后面添加內(nèi)容
suffixOverrides:?從后面刪除內(nèi)容
-->
<trim?prefix="set"?prefixOverrides="user"?suffix="hahaha"
suffixOverrides=",">
username=#{username},
</trim>
where?id=#{id}
</update>
?
6. <bind>
用于對(duì)數(shù)據(jù)進(jìn)行再加工, 用于模糊查詢
<select?id="sel"?resultType="user">
select?*?from?t_user
<where>
<if?test="username!=null and username!=''">
<bind name="username"?value="'%' + username + '%'"/>
and?username like?#{username}
</if>
</where>
</select>
?
7. <foreach>
用于在 SQL 語句中遍歷集合參數(shù), 在 in 查詢中使用
(1) collection: 待遍歷的集合
(2) open: 設(shè)置開始符號(hào)
(3) item: 迭代變量
(4) separator: 項(xiàng)目分隔符
(5) close: 設(shè)置結(jié)束符號(hào)
<select?id="selIn"?parameterType="list"?resultType="user">
select?*?from?t_user where?id in
<foreach collection="list"?open="("?separator=","?close=")"
item="item">
#{item}
</foreach>
</select>
?
List<User>?selIn(@Param("list")?List<Integer>?list);
?
8. <sql><include>
<sql>用于提取 SQL?語句,?<include>用于引用?SQL?語句
<sql?id="mySql">
id,?username,?password
</sql>
?
<select?id="selIn"?parameterType="list"?resultType="user">
select
<include refid="mySql"/>
from?t_user where?id in
<foreach collection="list"?open="("?separator=","?close=")"
item="item">
#{item}
</foreach>
</select>
轉(zhuǎn)載于:https://www.cnblogs.com/kendyho/p/10847940.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的3.3.10 动态SQL的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新闻网大数据实时分析可视化系统项目——7
- 下一篇: Flask入门到放弃(四)—— 数据库