经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MyBatis » 查看文章
mybatis update set 多个字段实例
来源:jb51  时间:2021/1/25 11:27:23  对本文有异议

我就废话不多说了,大家还是直接看代码吧~

  1. <update id="updateCustomer" parameterType="com.entrym.domain.Customer">
  2. UPDATE customer set
  3. <if test="name!=null">name=#{name,jdbcType=VARCHAR},</if>
  4. <if test="role!=null">role=#{role,jdbcType=VARCHAR},</if>
  5. <if test="userId != null">user_id = #{userId,jdbcType=INTEGER},</if>
  6. <if test="qq != null">qq = #{qq,jdbcType=VARCHAR},</if>
  7. <if test="mobile != null">mobile = #{mobile,jdbcType=VARCHAR}</if>
  8. WHERE id =#{id,jdbcType=BIGINT}

如果上面的mobile字段为null,执行下面的SQL语句

  1. UPDATE customer set name=?,role=?,userId=?,qq=?, where id=?

where 前面有逗号“,”就会报错

使用trim可以删掉最后字段的逗号“,”

set已被包含在trim中,所以不用重复写了:

  1. <update id="updateCustomer" parameterType="com.entrym.domain.Customer">
  2. UPDATE customer
  3. <trim prefix="set" suffixOverrides=",">
  4. <if test="claimTime!=null">claim_time=#{claimTime,jdbcType=VARCHAR},</if>
  5. <if test="claimState!=null">claim_state=#{claimState,jdbcType=INTEGER},</if>
  6. <if test="name!=null">name=#{name,jdbcType=VARCHAR},</if>
  7. <if test="role!=null">role=#{role,jdbcType=VARCHAR},</if>
  8. <if test="platformAccount!=null">platform_account=#{platformAccount,jdbcType=VARCHAR},</if>
  9. <if test="collaborateTime!=null">collaborate_time=#{collaborateTime,jdbcType=VARCHAR},</if>
  10. <if test="collaborateState!=null">collaborate_state=#{collaborateState,jdbcType=INTEGER},</if>
  11. <if test="userId != null">user_id = #{userId,jdbcType=INTEGER},</if>
  12. <if test="qq != null">qq = #{qq,jdbcType=VARCHAR},</if>
  13. <if test="mobile != null">mobile = #{mobile,jdbcType=VARCHAR}</if>
  14. </trim>
  15. WHERE id =#{id,jdbcType=BIGINT}
  16. </update>

转义字符:

&lt; 小于号 <

&gt; 大于号 >

&amp; 和 &

&apos; 单引号 '

&quot; 双引号 "

补充:Mybatis中update时set和if的用法

update时set和if的用法 每个修改都加逗号 set能够智能的去掉最后一个逗号。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。如有错误或未考虑完全的地方,望不吝赐教。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号