我想使用ibatis dbmapper.xml更新Postgres中的表。
条件是它应该仅更新映射包含值的列。对于其他列,不应触及该值。
假设我的公司表包含以下列{company_id,name,headQrtr,ceo}
如果我想使用company_id更新Company表中的任何特定行,我将使用以下代码片段(dbmapper.xml)
<update id="update_company_details" parameterType="map">
update Company set (name,headQrtr,ceo) values(${name_value},${headQrtr_value},${ceo_value}) where company_id=${company_id_value}
</update>
如果我更新所有三列,这样可以正常工作。
但是有些情况我只想更新ceo列,在这种情况下我的输入映射只有一个键(即ceo_value),其余的键不可用。 在这种情况下,其他列将使用空值进行更新。那我怎么能限制它?
我在考虑使用'if'标签,但不确定如何使用它来设置不同列的值?
答案 0 :(得分:0)
这对我有用......
<update id="update_company_details" parameterType="map">
UPDATE Company
<set>
<if test="name_value!= null">
name = #{name_value},
</if>
<if test="headQrtr_value!= null">
headQrtr= #{headQrtr_value},
</if>
<if test="ceo_value!= null">
ceo= #{ceo_value},
</if>
</set>
WHERE company_id= #{company_id_value}
</update>