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ù)寫(xiě)了:<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. 無(wú)責(zé)任Oracle圖書(shū)簡(jiǎn)評(píng) (1)2. MYSQL SQL查詢近7天一個(gè)月的數(shù)據(jù)的操作方法3. MySQL為什么要避免大事務(wù)以及大事務(wù)解決的方法4. Oracle災(zāi)難防護(hù)的關(guān)鍵技術(shù)5. Oracle 11g透明數(shù)據(jù)加密安全特性解析6. 輕松掌握Oracle數(shù)據(jù)庫(kù)Where條件執(zhí)行順序7. ?!皫?kù)”之路:Oracle數(shù)據(jù)庫(kù)性能保護(hù)8. 解讀Oracle中代替like進(jìn)行模糊查詢的方法instr(更高效)9. 在SQL Server數(shù)據(jù)庫(kù)中如何減少死鎖發(fā)生10. Mybatis 動(dòng)態(tài)表名+Map參數(shù)傳遞+批量操作詳解
