Mybatis中的updateByPrimaryKeySelective()


今天在工作中,又制造了一个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()就会根据传入的对象,全部取值插入数据库,会存在覆盖数据的问题;

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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM