- return productInfoMapper.updateByPrimaryKey(productInfo) > 0 ? true : false;
- return productInfoMapper.updateByPrimaryKeySelective(productInfo) > 0 ? true : false;
上面的是逆转工程生成的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)
这两个update都是使用generator生成的mapper.xml文件中,对dao层的更新操作
详细可以查看generator生成的源代码!
insert和insertSelective和上面类似,
insert:把所有值插入,但是要注意加入数据库字段有default,default是不会起作用的
insertSelective:只会插入有值的数据,并且不会忽略数据库字段有default的