1. 程式人生 > >Mybatis 中更新方法: updateByPrimaryKeySelective() 和 updateByPrimaryKey() 的區別

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

int updateByPrimaryKeySelective(TbItem record);
int updateByPrimaryKey(TbItem record);


上面的是逆轉工程生成的Mapper介面

對應的xml為

<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>

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

updateByPrimaryKey 對你注入的欄位全部更新,將為空的欄位在資料庫中置為NULL

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