首先:updateByExampleSelective(@Param(“record”) Xxx record, @Param(“example”) XxxExample example);
第一個參數 是要修改的部分值組成的對象,其中有些屬性爲null則表示該項不修改。
第二個參數 是一個對應的查詢條件的類, 通過這個類可以實現 order by 和一部分的where 條件。
使用方法大概如下:
public void edit(Long id, String name) {
Example example = new Example(Category.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("id",id);
Category category = new Category();
category.setName(name);
categoryMapper.updateByExampleSelective(category,example);
}
updateByExampleSelective是更新一條數據中的某些屬性,而不是更新整條數據。
而updateByExample需要將表的條件全部給出,也就是要給出一個對象,以下給出代碼:
<update id="updateByExample" parameterType="map" >
update tb_item
set id = #{record.id,jdbcType=BIGINT},
title = #{record.title,jdbcType=VARCHAR},
sell_point = #{record.sellPoint,jdbcType=VARCHAR},
price = #{record.price,jdbcType=BIGINT},
num = #{record.num,jdbcType=INTEGER},
barcode = #{record.barcode,jdbcType=VARCHAR},
image = #{record.image,jdbcType=VARCHAR},
cid = #{record.cid,jdbcType=BIGINT},
status = #{record.status,jdbcType=TINYINT},
created = #{record.created,jdbcType=TIMESTAMP},
updated = #{record.updated,jdbcType=TIMESTAMP}
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>