Mybatis之更新方法: updateByPrimaryKeySelective() 和 updateByPrimaryKey() 的區別


 

  1. int updateByPrimaryKeySelective(TbItem record);
  2.  
    int updateByPrimaryKey(TbItem record);


上面的是逆轉工程生成的Mapper接口

對應的xml為

  1.  
    <update id= "updateByPrimaryKeySelective" parameterType="com.taotao.pojo.TbItem">
  2.  
    update tb_item
  3.  
    < set>
  4.  
    < if test="title != null">
  5.  
    title = #{title,jdbcType=VARCHAR},
  6.  
    </ if>
  7.  
    </ set>
  8.  
    where id = #{id,jdbcType=BIGINT}
  9.  
    </update>
  10.  
    <update id= "updateByPrimaryKey" parameterType="com.taotao.pojo.TbItem">
  11.  
    update tb_item
  12.  
    set title = #{title,jdbcType=VARCHAR},
  13.  
    where id = #{id,jdbcType=BIGINT}
  14.  
    </update>

updateByPrimaryKeySelective 會對字段進行判斷再更新(如果為Null就忽略更新),如果你只想更新某一字段,可以用這個方法。

updateByPrimaryKey 對你注入的字段全部更新,將為空的字段在數據庫中置為NULL

轉自:https://blog.csdn.net/a670941001/article/details/54619432 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM