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,執(zhí)行下面的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> 轉(zhuǎn)義字符:
< 小于號 <
> 大于號 >
& 和 &
' 單引號 ’
" 雙引號 '
補充:Mybatis中update時set和if的用法

update時set和if的用法 每個修改都加逗號 set能夠智能的去掉最后一個逗號。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. 整理Oracle數(shù)據(jù)庫碎片2. Delphi中的Access技巧集3. MySQL中 concat函數(shù)的使用4. MYSQL數(shù)據(jù)庫存文本轉(zhuǎn)存數(shù)據(jù)庫問題5. MySQL Community Server 5.1.496. 導出錯誤編碼的mysql數(shù)據(jù)庫7. Mysql入門系列:MYSQL創(chuàng)建、刪除、索引和更改表8. 數(shù)據(jù)庫相關(guān)的幾個技能:ACCESS轉(zhuǎn)SQL9. 快速刪除ORACLE重復記錄10. 如何遠程調(diào)用ACCESS數(shù)據(jù)庫

網(wǎng)公網(wǎng)安備