Mybatis中如何一次性更新多个字段值,实现update set语句的多种字段修改?
- 内容介绍
- 文章标签
- 相关推荐
本文共计509个文字,预计阅读时间需要3分钟。
我这就简要改写一下,不超过100字:
不多说,直接看代码。更新客户信息,`updateCustomer`,参数类型`com.entrym.domain.Customer`。更新语句:`UPDATE customer SET if(test='name!=null') name=`
我就废话不多说了,大家还是直接看代码吧~
<update id="updateCustomer" 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 id="updateCustomer" 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能够智能的去掉最后一个逗号。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持易盾网络。如有错误或未考虑完全的地方,望不吝赐教。
本文共计509个文字,预计阅读时间需要3分钟。
我这就简要改写一下,不超过100字:
不多说,直接看代码。更新客户信息,`updateCustomer`,参数类型`com.entrym.domain.Customer`。更新语句:`UPDATE customer SET if(test='name!=null') name=`
我就废话不多说了,大家还是直接看代码吧~
<update id="updateCustomer" 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 id="updateCustomer" 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能够智能的去掉最后一个逗号。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持易盾网络。如有错误或未考虑完全的地方,望不吝赐教。

