mybatis update set 多個字段實例
我就廢話不多說了,大家還是直接看代碼吧~
<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,執行下面的SQL語句
UPDATE customer set name=?,role=?,userId=?,qq=?, where id=?
where 前面有逗號“,”就會報錯
使用trim可以刪掉最后字段的逗號“,”
set已被包含在trim中,所以不用重復寫了:<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> 轉義字符:
< 小于號 <
> 大于號 >
& 和 &
' 單引號 ’
" 雙引號 '
補充:Mybatis中update時set和if的用法
update時set和if的用法 每個修改都加逗號 set能夠智能的去掉最后一個逗號。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章: