mybatis從使用到了解(七)_mybatis動(dòng)態(tài)sql的妙用

動(dòng)態(tài)SQL

在使用JDBC拼接SQL的時(shí)候籽孙,經(jīng)常要確保不能完了必要的空格,對(duì)于的逗號(hào),而mybatis的動(dòng)態(tài)SQL則完美的解決了這些問題几颜。本文只介紹利用mybatis的動(dòng)態(tài)SQL解決常見的SQL拼接問題。
mybatis的動(dòng)態(tài)sql包含一下內(nèi)容:

  • if
  • choose,when,otherwise
  • trim,where,set
  • foreach
  • bind

解決where后SQL條件判斷問題


<select id="selectByParam" parameterType="int" resultMap="studentResult">
    select * from student where
    <if test="studentId != null">
        student_id = #{studentId}
    </if>
    <if test="studentAge != null">
        and student_age = #{studentAge}
    </if>
    <if test="studentPhone != null">
        and student_phone = #{studentPhone}
    </if>
</select>

在上名的sql中讯屈,如果三個(gè)if條件全為空蛋哭,則最后拼接的sql為:

select * from student where

如果第一個(gè)為判斷為空,則最后拼接的sql為:

select * from student where and student_age = #{studentAge} and student_phone = #{studentPhone}

上面拼接的兩個(gè)sql語法都存在問題涮母,只需要利用一點(diǎn)小技巧就能解決這個(gè)問題谆趾。如下,利用<where></where>標(biāo)簽叛本,mybatis會(huì)自動(dòng)處理上面的問題沪蓬。


<select id="selectByParam" parameterType="int" resultMap="studentResult">
    select  *   from student
    <where>
        <if test="studentId != null">
            student_id = #{studentId}
        </if>
        <if test="studentAge != null">
            and student_age = #{studentAge}
        </if>
        <if test="studentPhone != null">
            and student_phone = #{studentPhone}
        </if>
    </where>
</select>

也可以利用trim來解決

<select id="selectByParam" parameterType="int" resultMap="studentResult">
    select  *  from student
    <trim prefix="WHERE" prefixOverrides="AND |OR ">
        <if test="studentId != null">
            student_id = #{studentId}
        </if>
        <if test="studentAge != null">
            and student_age = #{studentAge}
        </if>
        <if test="studentPhone != null">
            and student_phone = #{studentPhone}
        </if>
    </trim>
</select>

利用<set>或<trim>解決update中set逗號(hào)問題

<update id = "updateById">
    update student
    <if test="studentName != null">student_name = #{studentName},</if>
    <if test="studentAge != null">student_age = #{studentAge},</if>
    <if test="studentPhone != null">student_name = student_phone = #{studentPhone},</if>
    where student_id = #{studentId}
</update>

從上面可以看出,set始終會(huì)多一個(gè)逗號(hào)来候。解決方案如下:

<update id = "updateById">
    update student
    <set>
        <if test="studentName != null">student_name = #{studentName},</if>
        <if test="studentAge != null">student_age = #{studentAge},</if>
        <if test="studentPhone != null">student_name = student_phone = #{studentPhone},</if>
    </set>
    where student_id = #{studentId}
</update>

或者

<update id = "updateById">
    update student
    <trim prefix="SET" suffixOverrides=",">
        <if test="studentName != null">student_name = #{studentName},</if>
        <if test="studentAge != null">student_age = #{studentAge},</if>
        <if test="studentPhone != null">student_name = student_phone = #{studentPhone},</if>
    </trim>
    where student_id = #{studentId}
</update>

利用foreach查詢

<select id="selectByIds" parameterType="int" resultMap="studentResult">
    select
    <include refid="studentSql"/>
    from student where student_id in
    <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
        #{item}
    </foreach>
</select>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末跷叉,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子营搅,更是在濱河造成了極大的恐慌云挟,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,820評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件转质,死亡現(xiàn)場(chǎng)離奇詭異园欣,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)休蟹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門沸枯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來日矫,“玉大人,你說我怎么就攤上這事绑榴∧慕危” “怎么了?”我有些...
    開封第一講書人閱讀 168,324評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵翔怎,是天一觀的道長(zhǎng)窃诉。 經(jīng)常有香客問我,道長(zhǎng)姓惑,這世上最難降的妖魔是什么褐奴? 我笑而不...
    開封第一講書人閱讀 59,714評(píng)論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮于毙,結(jié)果婚禮上敦冬,老公的妹妹穿的比我還像新娘。我一直安慰自己唯沮,他們只是感情好脖旱,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,724評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著介蛉,像睡著了一般萌庆。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上币旧,一...
    開封第一講書人閱讀 52,328評(píng)論 1 310
  • 那天践险,我揣著相機(jī)與錄音,去河邊找鬼吹菱。 笑死巍虫,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的鳍刷。 我是一名探鬼主播占遥,決...
    沈念sama閱讀 40,897評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼输瓜!你這毒婦竟也來了瓦胎?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,804評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤尤揣,失蹤者是張志新(化名)和其女友劉穎搔啊,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體芹缔,經(jīng)...
    沈念sama閱讀 46,345評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡坯癣,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,431評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了最欠。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片示罗。...
    茶點(diǎn)故事閱讀 40,561評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖芝硬,靈堂內(nèi)的尸體忽然破棺而出蚜点,到底是詐尸還是另有隱情,我是刑警寧澤拌阴,帶...
    沈念sama閱讀 36,238評(píng)論 5 350
  • 正文 年R本政府宣布绍绘,位于F島的核電站,受9級(jí)特大地震影響迟赃,放射性物質(zhì)發(fā)生泄漏陪拘。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,928評(píng)論 3 334
  • 文/蒙蒙 一纤壁、第九天 我趴在偏房一處隱蔽的房頂上張望左刽。 院中可真熱鬧,春花似錦酌媒、人聲如沸欠痴。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽喇辽。三九已至,卻和暖如春雨席,著一層夾襖步出監(jiān)牢的瞬間菩咨,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評(píng)論 1 272
  • 我被黑心中介騙來泰國打工陡厘, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留抽米,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,983評(píng)論 3 376
  • 正文 我出身青樓雏亚,卻偏偏與公主長(zhǎng)得像缨硝,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子罢低,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,573評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容