今天在工作中,又制造了一个bug,锅背好!不许动!o(╥﹏╥)o

原因是mybatis的updateByPrimaryKey()与updateByPrimaryKeySelective(),我没有搞清楚区别

<update id="updateByPrimaryKeySelective" parameterType="com.taotao.pojo.TbItem">
update tb_item
<set>
<if test="title != null">
title = #{title,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>

<update id="updateByPrimaryKey" parameterType="com.taotao.pojo.TbItem"> update tb_item set title = #{title,jdbcType=VARCHAR}, where id = #{id,jdbcType=BIGINT} </update>

查看工具生成的xml文件才发现,updateByPrimaryKeySelective()不会把null值插入数据库,避免覆盖之前有值的,

但是updateByPrimaryKey()就会根据传入的对象,全部取值插入数据库,会存在覆盖数据的问题;

具体使用哪一个还是要根据业务场景而使用。记住这个问题!

 

相关文章:

  • 2021-06-23
  • 2022-12-23
  • 2021-10-07
  • 2022-12-23
  • 2021-07-14
  • 2021-12-04
  • 2021-07-22
  • 2021-06-01
猜你喜欢
  • 2022-12-23
  • 2021-09-05
  • 2021-10-29
  • 2021-11-04
  • 2021-08-31
相关资源
相似解决方案