這篇文章將為大家詳細講解有關利用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.這種方式,可以一次執行多條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怎么對數據庫進行批量修改就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。