今天在工作中,又制造了一個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()就會根據傳入的對象,全部取值插入數據庫,會存在覆蓋數據的問題;
具體使用哪一個還是要根據業務場景而使用。記住這個問題!