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