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. MySQL 的啟動(dòng)和連接方式實(shí)例分析2. SQL SERVER數(shù)據(jù)庫(kù)開(kāi)發(fā)之存儲(chǔ)過(guò)程的應(yīng)用3. Docker部署Mysql集群的實(shí)現(xiàn)4. mybatis中方法返回泛型與resultType不一致的解決5. Oracle中pivot函數(shù)圖文實(shí)例詳解6. 在SQL Server 2005修改存儲(chǔ)過(guò)程7. 服務(wù)器Centos部署MySql并連接Navicat過(guò)程詳解8. SQL Server全文檢索簡(jiǎn)介9. idea連接SQL Server數(shù)據(jù)庫(kù)的詳細(xì)圖文教程10. Oracle?Users表空間重命名問(wèn)題解決
