mybatis update set 多個(gè)字段實(shí)例
我就廢話不多說(shuō)了,大家還是直接看代碼吧~
<update parameterType='com.entrym.domain.Customer'> UPDATE customer set <if test='name!=null'>name=#{name,jdbcType=VARCHAR},</if> <if test='role!=null'>role=#{role,jdbcType=VARCHAR},</if> <if test='userId != null'>user_id = #{userId,jdbcType=INTEGER},</if> <if test='qq != null'>qq = #{qq,jdbcType=VARCHAR},</if> <if test='mobile != null'>mobile = #{mobile,jdbcType=VARCHAR}</if> WHERE id =#{id,jdbcType=BIGINT}
如果上面的mobile字段為null,執(zhí)行下面的SQL語(yǔ)句
UPDATE customer set name=?,role=?,userId=?,qq=?, where id=?
where 前面有逗號(hào)“,”就會(huì)報(bào)錯(cuò)
使用trim可以刪掉最后字段的逗號(hào)“,”
set已被包含在trim中,所以不用重復(fù)寫了:<update parameterType='com.entrym.domain.Customer'> UPDATE customer <trim prefix='set' suffixOverrides=','> <if test='claimTime!=null'>claim_time=#{claimTime,jdbcType=VARCHAR},</if> <if test='claimState!=null'>claim_state=#{claimState,jdbcType=INTEGER},</if> <if test='name!=null'>name=#{name,jdbcType=VARCHAR},</if> <if test='role!=null'>role=#{role,jdbcType=VARCHAR},</if> <if test='platformAccount!=null'>platform_account=#{platformAccount,jdbcType=VARCHAR},</if> <if test='collaborateTime!=null'>collaborate_time=#{collaborateTime,jdbcType=VARCHAR},</if> <if test='collaborateState!=null'>collaborate_state=#{collaborateState,jdbcType=INTEGER},</if> <if test='userId != null'>user_id = #{userId,jdbcType=INTEGER},</if> <if test='qq != null'>qq = #{qq,jdbcType=VARCHAR},</if> <if test='mobile != null'>mobile = #{mobile,jdbcType=VARCHAR}</if> </trim> WHERE id =#{id,jdbcType=BIGINT}</update> 轉(zhuǎn)義字符:
< 小于號(hào) <
> 大于號(hào) >
& 和 &
' 單引號(hào) ’
" 雙引號(hào) '
補(bǔ)充:Mybatis中update時(shí)set和if的用法
update時(shí)set和if的用法 每個(gè)修改都加逗號(hào) set能夠智能的去掉最后一個(gè)逗號(hào)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. ORACLE中常用的幾種正則表達(dá)式小結(jié)2. python之sqlalchemy創(chuàng)建表的實(shí)例詳解3. SQLite數(shù)據(jù)庫(kù)安裝及基本操作指南4. mysql中的utf8與utf8mb4存儲(chǔ)及區(qū)別5. short int、long、float、double使用問(wèn)題說(shuō)明6. Access轉(zhuǎn)Sql Server問(wèn)題 實(shí)例說(shuō)明7. sql查詢給表起別名要點(diǎn)小結(jié)(涉及嵌套查詢)8. Access 使用總結(jié)一篇9. SQLite3數(shù)據(jù)庫(kù)的介紹和使用教程(面向業(yè)務(wù)編程-數(shù)據(jù)庫(kù))10. sqlite時(shí)間戳轉(zhuǎn)時(shí)間語(yǔ)句(時(shí)間轉(zhuǎn)時(shí)間戳)
