mybatis批量更新
生活随笔
收集整理的這篇文章主要介紹了
mybatis批量更新
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最開始的時候,想著寫一系列并列的更新語句就可以了
<update id="updateBatch" parameterType="java.util.List"><foreach collection="list" item="item" index="index" separator=";"open="" close="">update REGION_CODE setCODE=#{item.Code,jdbcType=VARCHAR},NAME=#{item.Name,jdbcType=VARCHAR}where ID = #{item.id,jdbcType=DECIMAL}</foreach>
</update>
這樣直接報錯,因為Mybatis映射文件中的sql語句不允許 ; 符號。按照可行的case when處理方式,Mybatis映射文件書寫方式如下:
<update id="updateBatch" parameterType="java.util.List">update REGION_CODE setCODE=<foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">when #{item.id,jdbcType=DECIMAL} then #{item.Code,jdbcType=VARCHAR}</foreach>,NAME=<foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">when #{item.id,jdbcType=DECIMAL} then #{item.Name,jdbcType=VARCHAR}</foreach>where ID in<foreach collection="list" index="index" item="item" separator="," open="(" close=")">#{item.id,jdbcType=DECIMAL}</foreach>
</update>至此,批量更新功能完成。
總結
以上是生活随笔為你收集整理的mybatis批量更新的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js面向对象写法,一个小例子
- 下一篇: json用法示例