• 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()就会根据传入的对象,全部取值插入数据库,会存在覆盖数据的问题;

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

  • 相关阅读:
    诸侯安置
    可可西里
    直线交点
    切割多边形
    ACM挑战程序设计竞赛1.1抽签
    朝鲜战争:轰炸大小和岛
    星际争霸II 战斗问题
    乒乓球
    瑞士轮
    NOIP 2010 普及组解题报告
  • 原文地址:https://www.cnblogs.com/runwithraining/p/10884897.html
Copyright © 2020-2023  润新知