日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

3.3.10 动态SQL

發(fā)布時(shí)間:2023/12/10 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 3.3.10 动态SQL 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

十、動(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。