对于list更新sql语句的写法
批量更新
<update id="updateConfigureNames" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open=""
close="" separator=";">
update configure
<set>
configure_name=#{item.configureName}
</set>
where id = #{item.id,jdbcType=VARCHAR}
</foreach>
</update>在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的
主要有一下3种情况:
- 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
- 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
- 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可
mybatis动态更新sql语句
注意参数是实体
<!--更新-->
<update id="updateOne" parameterType="com.entity.GovernmentDetail">
UPDATE GOVERNMENT_DETAIL
<trim prefix="set" suffixOverrides=",">
<if test="UNIT_TYPE!=null">unitType=#{unitType},</if>
<if test="PLAT_CODE!=null">plateCode=#{plateCode},</if>
<if test="DESCRIP!=null">descrip=#{descrip},</if>
<if test="BUSINESS_GUID!=null">businessGuid=#{businessGuid},</if>
<if test="HOT_LINE!=null">hotLine=#{hotLine},</if>
<if test="LINE_ADRESS!=null">lineAdress=#{lineAdress},</if>
<if test="EXTEND1!=null">extend1=#{extend1},</if>
<if test="EXTEND2!=null">extend2=#{extend2},</if>
</trim>
WHERE ID=#{id}
</update>以上为个人经验,希望能给大家一个参考,也希望大家多多支持Devmax。