Mybatis批量修改的操作代碼
1.修改的字段值都是一樣的,id不同
<update id="batchUpdate" parameterType="String">
update cbp_order
set status=1
where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</update>
---參數說明---
collection:表示類型,就寫成array,如果是集合,就寫成list
?item? : 是一個變量名,自己隨便起名
2.這種方式,可以一次執(zhí)行多條SQL語句
<update id="batchUpdate" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update test
<set>
test=#{item.test}+1
</set>
where id = #{item.id}
</foreach>
</update>
3.整體批量更新
<update id="updateBatch" parameterType="java.util.List">
update mydata_table
<trim prefix="set" suffixOverrides=",">
<trim prefix="status =case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.status !=null and item.status != -1">
when id=#{item.id} then #{item.status}
</if>
<if test="item.status == null or item.status == -1">
when id=#{item.id} then mydata_table.status//原數據
</if>
</foreach>
</trim>
</trim>
where id in
<foreach collection="list" index="index" item="item" separator="," open="(" close=")">
#{item.id,jdbcType=BIGINT}
</foreach>
</update>
----<trim>屬性說明-------
1.prefix,suffix 表示在trim標簽包裹的部分的前面或者后面添加內容
2.如果同時有prefixOverrides,suffixOverrides 表示會用prefix,suffix覆蓋Overrides中的內容。
3.如果只有prefixOverrides,suffixOverrides 表示刪除開頭的或結尾的xxxOverides指定的內容。
總結
以上所述是小編給大家介紹的Mybatis批量修改的操作代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
相關文章
java實現(xiàn)基于UDP協(xié)議網絡Socket編程(C/S通信)
這篇文章主要介紹了java實現(xiàn)基于UDP協(xié)議網絡Socket編程(C/S通信),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10
SpringBoot應用整合ELK實現(xiàn)日志收集的示例代碼
這篇文章主要介紹了SpringBoot應用整合ELK實現(xiàn)日志收集的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09

