今天在工作中,又制造了一个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()就会根据传入的对象,全部取值插入数据库,会存在覆盖数据的问题;
具体使用哪一个还是要根据业务场景而使用。记住这个问题!